------=_NextPart_000_004C_01C8ADE9.8884D110 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: quoted-printable Hi, How do I simplify the regex below so that it matches only the number 1, = henceforth it should return false if I match $string with $match. use strict; use warnings; my $string =3D "10 11 12 13 40"; my $match =3D 1; if ($string =3D~/^$match | $match | $match$/g){ print "match"; }else{ print "unmatch"; }; ------=_NextPart_000_004C_01C8ADE9.8884D110--
![]() |
0 |
![]() |
On Sun, May 4, 2008 at 1:19 AM, <itshardtogetone@hotmail.com> wrote: > Hi, > How do I simplify the regex below so that it matches only the number 1, henceforth it should return false if I match $string with $match. > > use strict; > use warnings; > > my $string = "10 11 12 13 40"; > my $match = 1; > > if ($string =~/^$match | $match | $match$/g){ > print "match"; > }else{ > print "unmatch"; > }; > I may be missing something, but I believe you will get the same results with just if ($string =~ /$match/) { print "match\n"; } else { print "no match\n"; } However, if you are going to include a string in a regex you should probably use the \Q and \E modifiers* to prevent any metacharacters (., *, ?, etc.) in the string from being interpreted by the regex (unless, of course, that is what you want): if ($string =~ /\Q$match\E/) { print "match\n"; } else { print "no match\n"; } If you are going to use the same string in a regex multiple times you are better off compiling it as a regex with the qr// operator** first: my $match = qr/1/; * http://perldoc.perl.org/perlre.html#Regular-Expressions ** http://perldoc.perl.org/perlop.html#qr%2fSTRING%2fmsixpo -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read.
![]() |
0 |
![]() |
itshardtogetone@hotmail.com wrote: > How do I simplify the regex below so that it matches only the number > 1, henceforth it should return false if I match $string with $match. > > use strict; > use warnings; > > my $string = "10 11 12 13 40"; > my $match = 1; > > if ($string =~/^$match | $match | $match$/g){ Do you possibly mean something like: my $match = qr(\b1\b); if ( $string =~ /$match/ ) { which would be true for the string my $string = "10 11 1 13 40"; but not for your original string. -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
![]() |
0 |
![]() |
On Sun, May 4, 2008 at 1:45 AM, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote: snip > > if ($string =~/^$match | $match | $match$/g){ snip > my $match = qr(\b1\b); snip Ah, I was missing something, the spaces. This is much better than my answer. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read.
![]() |
0 |
![]() |
Chas. Owens wrote: > On Sun, May 4, 2008 at 1:45 AM, Gunnar Hjalmarsson <noreply@gunnar.cc> wrote: > snip >>> if ($string =~/^$match | $match | $match$/g){ > snip >> my $match = qr(\b1\b); > snip > > Ah, I was missing something, the spaces. This is much better than my answer. I think this is even safer: my $match = qr"(?:^|\s)1(?:\s|$)"; -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
![]() |
0 |
![]() |