Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Verifying different substrings

Posted on 2012-03-18
4
Medium Priority
?
228 Views
Last Modified: 2012-03-18
Hello,

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

0
Comment
Question by:Marc_Engrie
[X]
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
4 Comments
 
LVL 7

Expert Comment

by:micropc1
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

0
 

Author Comment

by:Marc_Engrie
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 :-)
0
 
LVL 7

Accepted Solution

by:
micropc1 earned 375 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...
0
 

Author Comment

by:Marc_Engrie
ID: 37735203
Yep, it matches !

Thx
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…
Six Sigma Control Plans

705 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