• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Regular Expression  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Appreciate your help in helping me with regular expression for below 2 files containing the keyword RES & UDR. The requirement is to pick only the XML files from a directory having the keyword RES or UDR ( or both). I am trying to find out regular expression matching this criteria.


QM_2017121114173526400_WI90_KM_UDR.xml

QM_2017022019000817400_WI90_KM_RES.xml

 
Saloon Keeper
Posts: 4856
117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While a regex would work, why not check filename.contains("_UDR_") and filename.contains("_RES_")? If you keep the list of string to search in an array, the approach scales easily.
 
Varun Thonse Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Well, I would need REGEX since this would be used in MQ MFT monitor to pick the files from directory and its not java program.
 
Saloon Keeper
Posts: 4988
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If URD/RES appear at the end of the file name:
".*_UDR\.xml"
".*_RES\.xml"


If UDR/RES can appear anywhere:
".*UDR.*"
".*RES.*"


I would need refinement of your requirements if this is not what you had in mind.
 
Varun Thonse Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The RES and UDR keyword can appear anywhere.

Here is the thing. I have a directory say F:\csdata\MessageBus\Maydown_Yarn\UploadQM where two types of files would be present ( say QM_2017022019000817400_WI90_KM_RES.xml & QM_2017121114173526400_WI90_KM_UDR.xml)

I have a MQ MFT ( Product from IBM MQ) which is file transfer product. The MQ MFT resource monitor would allow to pick the  file from the monitored directory either using wild card or java regular expression. ( Please see attached screenshot)

I am looking for regular expression which I can write in "Match Pattern" column to instruct the monitor to pick the file having either "RES" or "UDR" keyword.



MQ_MFT.png
[Thumbnail for MQ_MFT.png]
 
Tim Moores
Saloon Keeper
Posts: 4856
117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, something like

.*_UDR\.xml$|.*_RES\.xml$

should work. The syntax might differ a bit, but maybe not.
 
Saloon Keeper
Posts: 9378
181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use ^.*(?:RES|UDR).*+$, if RES or UDR can appear truly anywhere in the file name. This causes only one quantifier to backtrack, giving you linear performance rather than quadratic.
 
Varun Thonse Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan & Tim. I tried both the options and it worked. I would use ^.*(?:RES|UDR).*+$ since the keyword RES and UDR can appear anywhere in the file name.
 
Stephan van Hulst
Saloon Keeper
Posts: 9378
181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand it? Do you know what all the components of this regex do and why they were chosen?
 
lowercase baba
Bartender
Posts: 12628
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
question...why do you need the anchors?  if you start and end your expression with ".*", do those actually matter?
 
Varun Thonse Rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

^.*(?:RES|UDR).*+$

^.* - Indicates the beginning of the file name can start with any characters (except line break) zero or more times

(?:RES|UDR) - Indicates the keyword RES "OR" UDR appearing zero or one time

.* - RES or UDR would be present zero or more times 

+  - matches the preceding character one or more times

^ and $ - Indicates  whatever is in between them must cover the entire line end-to-end
 
Stephan van Hulst
Saloon Keeper
Posts: 9378
181
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:question...why do you need the anchors?  if you start and end your expression with ".*", do those actually matter?


It more accurately represents what you want to look for. Also, you can use it to find multiple filenames if you use it on a list of filenames separated by line separators.

Varun Thonse Rao wrote:Yes.

+  - matches the preceding character one or more times


Why would I want to do that if I already used the * quantifier.

No, .*+ means any character, zero or more times, as much as possible, without backtracking. The + symbol makes the quantifier possessive.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!