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: Regular Expressions:
match a string with 4 digits but not 1111

 



LarryLoser
New User

Jan 18, 2013, 6:20 PM

Post #1 of 4 (9867 views)
match a string with 4 digits but not 1111 Can't Post

I am having a brain cramp.

How can I match a string with 4 consecutive digits so long as they are not 1111?

dsjhsd 1234 dfddf = match

sdadad1114jkgjhg = match

dassfa1111jhgj = no match

sdad123dfasfd = no match


LarryLoser
New User

Jan 18, 2013, 7:58 PM

Post #2 of 4 (9863 views)
Re: [LarryLoser] match a string with 4 digits but not 1111 [In reply to] Can't Post

I got it

$string =~ /(?!1111)\d{4}/


BillKSmith
Veteran

Jan 18, 2013, 8:41 PM

Post #3 of 4 (9861 views)
Re: [LarryLoser] match a string with 4 digits but not 1111 [In reply to] Can't Post

That sure does not look right, but I cannot make it fail. Could anyone please explain.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Jan 18, 2013, 9:48 PM

Post #4 of 4 (9857 views)
Re: [BillKSmith] match a string with 4 digits but not 1111 [In reply to] Can't Post


Code
use strict; 
use warnings;
use v5.10.1;
use YAPE::Regex::Explain;

my $REx = '/(?!1111)\d{4}/';
say YAPE::Regex::Explain->new($REx)->explain;



Code
The regular expression: 

(?-imsx:/(?!1111)\d{4}/)

matches as follows:

NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
/ '/'
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
1111 '1111'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
----------------------------------------------------------------------
/ '/'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------


 
 


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

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