CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
RegEx with 'and'

 



pdolinski
Novice

Aug 20, 2015, 6:41 AM

Post #1 of 4 (1416 views)
RegEx with 'and' Can't Post

hi. I am trying to build an expr that will validate database names in our system.

The requirement is that the name must start with a letter, followed by any combination of letters, digits or underscores, provided that there can be no two or more underscores in sequence.
I don't care whether the regex allows the string to end with '_' or not.

good: mydb_cli_6
bad: yourdb__dli_7

my would-be solution: m/^[a-z]+[(?=_)(?=[a-z\d]+)]*$/i

Logic:
start with 1 or more chars [a-z]+
followed by the block [(?=_)(?=[a-z\d]+)]* made optional with the '*' char
In the optional block, if there is an underscore, it must be followed by 1 or more chars or digits.

Why does Perl think there is a syntax error here due to unmatched ')'? How do I rewrite this to Perl's liking?

Thanks.

Pete


FishMonger
Veteran / Moderator

Aug 20, 2015, 7:38 AM

Post #2 of 4 (1411 views)
Re: [pdolinski] RegEx with 'and' [In reply to] Can't Post

The first problem I see is that you're putting positive lookaheads inside a character class. That's not allowed.


(This post was edited by FishMonger on Aug 20, 2015, 7:50 AM)


FishMonger
Veteran / Moderator

Aug 20, 2015, 7:48 AM

Post #3 of 4 (1409 views)
Re: [pdolinski] RegEx with 'and' [In reply to] Can't Post

You could achieve your goal with a single complex regex, but in this case, I'd use 2 simple regex's.


Code
my @name = ('mydb_cli_6', 'yourdb__dli_7'); 

foreach (@name) {
if ( /^[a-z]+\w+$/i and not /_{2,}/) {
print "$_ is ok\n";
}
else {
print "$_ is BAD\n";
}
}


/_{2,}/ could also be written as /__/


(This post was edited by FishMonger on Aug 20, 2015, 7:51 AM)


pdolinski
Novice

Aug 20, 2015, 9:15 AM

Post #4 of 4 (1404 views)
Re: [pdolinski] RegEx with 'and' [In reply to] Can't Post

This will do. Thanks.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives