Solved

A doubt in regex

Posted on 2011-09-07
16
288 Views
Last Modified: 2012-05-12
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
Comment
Question by:pvinodp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +3
16 Comments
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 36494772
$archive =~ /Merry\.Rolls\.([GS]R\d+\.\d+.\d+)\.(\w+).tgz/;

Open in new window

0
 

Author Comment

by:pvinodp
ID: 36494795
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
 

Author Comment

by:pvinodp
ID: 36494807
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 36494822
/Merry\.Rolls\.(GR\d+\.\d+.\d+)\.(\w+(?:\.D)?).tgz/;
0
 

Author Comment

by:pvinodp
ID: 36494914
what is the meaning of (?:\D)?  
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 36495196
(?:\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
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 100 total points
ID: 36497873
Check this:
$archive =~ /Merry\.Rolls\.(GR\d+\.\d+.\d+)\.(\w+)(\.D)?.tgz/;

Open in new window

That will allow optional .D
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36498217
@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
 
LVL 63

Expert Comment

by:Zvonko
ID: 36498353
I see a difference.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36498752
How enlightening...
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36498819
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36498863
Where do you see a lookahead?
0
 
LVL 84

Expert Comment

by:ozo
ID: 36498886
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
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 50 total points
ID: 36500478
Zvonko, I think you confused the ?: in (?:\.D)? for a lookahead. Actually, the ?: just makes the group into a non-capturing group.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36502369
Awww...   Terry, you spoiled the surprise  8-P
0
 

Author Closing Comment

by:pvinodp
ID: 36535576
thanks all
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question