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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Clean URL from GET variables 2 93
Formatting stings with pack and printf in perl 5 68
numeric only entry in text inpu 9 43
perl: Cleaning meta tags using RegEX 12 63
by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now