Validating varying substrings

Posted on 2012-03-19
Medium Priority
Last Modified: 2012-06-21

I am not that experienced in regex and I struggle with the following

$string = 'DEFAULT:1:  TITLE:5    AXIS:6:     UNIT:13:     LEGEND:21:   WATERMARK:1: ';
print "yes\n" if ($string =~ /^[^#]*:[0-9]+:[^#]*( +[^#]*:[0-9]+:[^#]*)*$/i)

Open in new window

This code should accept TEXT:9:TEXT  or  TEXT:99:  or  TEXT:2:"TEXT"   but should not accept
TEXT:9 or   TEXT:   or  TEXT.

The regex I have here does accept TEXT:5  which it shouldn't  :-)
Question by:Marc_Engrie
  • 2
LVL 85

Accepted Solution

ozo earned 500 total points
ID: 37736634
/^[^#:]*:[0-9]+:[^#:]*( +[^#:]*:[0-9]+:[^#:]*)*$/

Author Comment

ID: 37737272
Yes !!!

However I do not fully understand [^#:]*:

Can you enlighten me on this so I learn something today ;-)  

Thx a lot
LVL 27

Expert Comment

ID: 37737896
[^#:]* says to match 0 or more characters that are not # or :

Author Comment

ID: 37737910
Understood and learned !

Thx :-)

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

619 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