Verifying different substrings

Posted on 2012-03-18
Last Modified: 2012-03-18

I want to verify in a string various substrings. They are all in a format  TEXT#RRGGBB but optinal one should be able to provide TEXT#RRGGBBTT  where TT varies from 00 to FF (transparent setting).
My current code looks like this
$string = 'BLACK#112233 BLUE#445566 GREEN#123456 TEST#112233';
print "yes\n" if ($string =~ /^[^#]*#[0-9a-f]{6}( +[^#]*#[0-9a-f]{6})*$/i );

Open in new window

So how do I have to change the regex expression to obtain a 'yes' for a string like this?
$string = 'BLACK#11223344 BLUE#445566 GREEN#123456 TEST#112233FF';
print "yes\n" if ($string =~ /^[^#]*#[0-9a-f]{6}( +[^#]*#[0-9a-f]{6})*$/i );

Open in new window

Question by:Marc_Engrie
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
  • 2
  • 2

Expert Comment

ID: 37734974
I think this should do it...

$string = 'BLACK#11223344 BLUE#445566 GREEN#123456 TEST#112233FF';
print "yes\n" if ($string =~ /^[^#]*#[0-9a-fA-F]{6}[0-9a-fA-F]{0,2}( +[^#]*#[0-9a-fA-F]{6}[0-9a-fA-F]{0,2})*$/i );

Open in new window


Author Comment

ID: 37735028
Not quite yet.

It will allow GREEN#1234561   which should not be allowed as there is only 1 extra character instead of 0 or 2  so 1 should not be allowed.

BTW:  a-fA-F  can be replace by a-f  because of the /i at the end :-)

Accepted Solution

micropc1 earned 125 total points
ID: 37735143
Ah..right. Try...

$string = 'BLACK#11223344 BLUE#445566 GREEN#123456 TEST#112233FF';
print "yes\n" if ($string =~ /^[^#]*#[0-9a-f]{6}([0-9a-f][0-9a-f]){0,1}( +[^#]*#[0-9a-f]{6}([0-9a-f][0-9a-f]){0,1})*$/i );

Open in new window

I've never used /i - learned something new there...

Author Comment

ID: 37735203
Yep, it matches !


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

728 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