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
Solved

Verifying different substrings

Posted on 2012-03-18
4
219 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
  • 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 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...
0
 

Author Comment

by:Marc_Engrie
ID: 37735203
Yep, it matches !

Thx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

808 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