Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

A doubt in regex

my requirement:
 $archive =~ /Merry\.Rolls\.(GR\d+\.\d+.\d+)\.(\w+).tgz/;

$archive can be:
Merry.Rolls.SR2.1.0.07Sep11_082115.tgz
OR
Merry.Rolls.GR2.1.0.07Sep11_082115.D.tgz

The second one fails. What shpuld be my regex?
The D in the second one can be any other letter.
0
pvinodp
Asked:
pvinodp
  • 5
  • 4
  • 3
  • +3
4 Solutions
 
Kamaraj SubramanianApplication Support AnalystCommented:
$archive =~ /Merry\.Rolls\.([GS]R\d+\.\d+.\d+)\.(\w+).tgz/;

Open in new window

0
 
pvinodpAuthor Commented:
Sorry that was a typo:
$archive can be:
Merry.Rolls.GR2.1.0.07Sep11_082115.tgz
OR
Merry.Rolls.GR2.1.0.07Sep11_082115.D.tgz
0
 
pvinodpAuthor Commented:
basically what i want is how to write a regex which could either be \w followed by a . or nothing..

ie:
mytext.D.tgz OR mytext.tgz
0
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
ozoCommented:
/Merry\.Rolls\.(GR\d+\.\d+.\d+)\.(\w+(?:\.D)?).tgz/;
0
 
pvinodpAuthor Commented:
what is the meaning of (?:\D)?  
0
 
käµfm³d 👽Commented:
(?:\D)
That means:

(?: ... )    Non-capturing group
\D           Any character NOT a digit

Open in new window


However, ozo's pattern is actually:

(?:\.D)

Open in new window


which means:

(?: ... )    Non-capturing group
\.           Literal period
D            Literal "D"

Open in new window


Notice the difference a character can make! The question mark following the non-capturing group makes the whole group optional (i.e. 0 or 1 occurrences).
0
 
ZvonkoSystems architectCommented:
Check this:
$archive =~ /Merry\.Rolls\.(GR\d+\.\d+.\d+)\.(\w+)(\.D)?.tgz/;

Open in new window

That will allow optional .D
0
 
käµfm³d 👽Commented:
@Zvonko
That is identical to what ozo already posted ( http:#36494822 ), save a difference in grouping. There is no functional difference between your pattern and ozo's pattern   : \
0
 
ZvonkoSystems architectCommented:
I see a difference.
0
 
käµfm³d 👽Commented:
How enlightening...
0
 
ZvonkoSystems architectCommented:
Sorry, I withdrow my comment http:#36497873

I posted it mainly to show how the lookahead pattern can be avoided.

But first of all Perl understands lookahead and second I just tested it also in IE browser and now even IE understands lookahead patterns in RegExp Objects.
So there is no reason for avoiding it.


0
 
käµfm³d 👽Commented:
Where do you see a lookahead?
0
 
ozoCommented:
the original question does not specify what captures are desired in the case of Merry.Rolls.GR2.1.0.07Sep11_082115.D.tgz
it may be that
/Merry\.Rolls\.(GR\d+\.\d+.\d+)\.(\w+)(\.D)?.tgz/
would be preferable
0
 
Terry WoodsIT GuruCommented:
Zvonko, I think you confused the ?: in (?:\.D)? for a lookahead. Actually, the ?: just makes the group into a non-capturing group.
0
 
käµfm³d 👽Commented:
Awww...   Terry, you spoiled the surprise  8-P
0
 
pvinodpAuthor Commented:
thanks all
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now