Home > Archive > PERL Miscellaneous > July 2004 > regex problem with with '|'
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
regex problem with with '|'
|
|
| Sharif Islam 2004-07-28, 9:01 pm |
| I know this is something very simple, but I can't get the right pattern.
#!/usr/bin/perl -w
use strict;
my @array;
@array=('http://www.url.com?db=hph&db=xyz&jid=ABC%22','http://www.direct.asp?db=npx&db=uib&jn=%22PAW%22',
'http://www.www.com?db=abc&jid=HTY');
print "@array\n";
foreach (@array) {
my @newurl = /db=(.{3})/g ;
my @code1 = /jn|jid=([^%22]{3})/ ;
print @newurl;
print @code1;
print "\n";
}
___OUTPUT___
# perl match.pl
http://www.url?db=hph&db=xyz&jid=ABC%22
http://www.direct.asp?db=npx&db=uib&jn=%22PAW%22
http://www.www.com?db=abc&jid=HTY
hphxyzABC
Use of uninitialized value in print at match.pl line 14.
npxuib
abcHTY
____END OUTPUT___
I want hphxyzABC
npxuibPAW
abcHTY
Why the jn or jid part not working?
Thanks,
--
Sharif
| |
| Greg Bacon 2004-07-28, 9:01 pm |
| In article <ce5via$ju6$1@news.ks.uiuc.edu>,
Sharif Islam <mislam@uiuc.edu> wrote:
: [...]
: I want hphxyzABC
: npxuibPAW
: abcHTY
Remember that you need to group the alternatives when that's what
you mean:
$ cat try
#! /usr/local/bin/perl
use warnings;
use strict;
my @array = (
'http://www.url.com?db=hph&db=xyz&jid=ABC%22',
'http://www.direct.asp?db=npx&db=uib&jn=%22PAW%22',
'http://www.www.com?db=abc&jid=HTY',
);
$" = "]["; # mjd's array printing trick
for (@array) {
print "$_ matches:\n";
my @newurl = /db=(.{3})/g;
my @code1 = /(?:jn|jid)=(?:%22)?(.{3})/g;
print " \@newurl = [@newurl]\n";
print " \@code1 = [@code1]\n";
}
[gbacon@cslwww gbacon]$ ./try
http://www.url.com?db=hph&db=xyz&jid=ABC%22 matches:
@newurl = [hph][xyz]
@code1 = [ABC]
http://www.direct.asp?db=npx&db=uib&jn=%22PAW%22 matches:
@newurl = [npx][uib]
@code1 = [PAW]
http://www.www.com?db=abc&jid=HTY matches:
@newurl = [abc]
@code1 = [HTY]
Hope this helps,
Greg
--
... history consists of a series of swindles, in which the masses are
first lured into revolt by the promise of Utopia, and then, when they
have done their job, enslaved over again by new masters.
-- George Orwell
| |
| Joe Smith 2004-07-28, 9:01 pm |
| Sharif Islam wrote:
> I know this is something very simple, but I can't get the right pattern.
> my @code1 = /jn|jid=([^%22]{3})/ ;
You've got a misunderstanding there. Inside of square brackets, "^" means
a negation of the character class that follows, not beginning of string.
/[^%22]{3}/ is the same as /[^2%]{3}/;
it matches any three characters that are not "%" and not "2".
You'll need to use parens, as Greg Bacon showed.
-Joe
|
|
|
|
|