Solved

Validating varying substrings

Posted on 2012-03-19
4
163 Views
Last Modified: 2012-06-21
Hello,

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  :-)
0
Comment
Question by:Marc_Engrie
  • 2
4 Comments
 
LVL 84

Accepted Solution

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

Author Comment

by:Marc_Engrie
ID: 37737272
Yes !!!

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

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

Thx a lot
0
 
LVL 26

Expert Comment

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

Author Comment

by:Marc_Engrie
ID: 37737910
Understood and learned !

Thx :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
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…

825 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