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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Writing a parser for java language 4 91
Python Regex Problem 24 147
Control Number of Log Files -Perl 7 94
How to translate this 2-line while loop into C from Perl? 8 137
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

733 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