Validating varying substrings

Marc_Engrie
Marc_Engrie used Ask the Experts™
on
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  :-)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2014
Top Expert 2015
Commented:
/^[^#:]*:[0-9]+:[^#:]*( +[^#:]*:[0-9]+:[^#:]*)*$/

Author

Commented:
Yes !!!

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

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

Thx a lot
[^#:]* says to match 0 or more characters that are not # or :

Author

Commented:
Understood and learned !

Thx :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial