match aaa but not 123aaa

K

Ken Sington

i have these arbitary strings
"YaaaY" and "123YaaaY"
I'm having trouble matching only if "aaa" exist between Ys without
having 123 preceeding it.

example:
"YaaaY" --> pass
" YaaaY " --> pass
"XXXYaaaYXXX" --> pass
"alskdjlkajf 123YaaaY laskdjf" --> fail

my one liner didn't work
$x =~ s/(!\123)(Y)(a{3})(Y)/$1$2$3$4/g;
the ! doesn't seem to negate it.

for an extra bonus, how about if I wanted "aaa" to go between "[" and
"]"? or ( and )?

what don't I see here??
 
M

Matt Garrish

Ken Sington said:
i have these arbitary strings
"YaaaY" and "123YaaaY"
I'm having trouble matching only if "aaa" exist between Ys without
having 123 preceeding it.

example:
"YaaaY" --> pass
" YaaaY " --> pass
"XXXYaaaYXXX" --> pass
"alskdjlkajf 123YaaaY laskdjf" --> fail

What about 123abcYaaaY? Is that a pass or fail?
my one liner didn't work
$x =~ s/(!\123)(Y)(a{3})(Y)/$1$2$3$4/g;
the ! doesn't seem to negate it.

Why are you using a substitution if you're trying to test whether '123'
precedes 'YaaaY' or not? And if you don't want '123', why capture it *and*
use it in the replacement? I would recommend something like the following:

if ( ( $x =~ /YaaaY/ ) && ( $` !~ /123$/ ) ) {
print $x;
}

If '123' can appear anywhere before 'YaaaY', just don't anchor the numbers
to the end of the pre-match.
for an extra bonus, how about if I wanted "aaa" to go between "[" and
"]"? or ( and )?

In the substitution or you have going above or are you back to matching
three a's again?

Matt
 
J

Jay Tilton

: i have these arbitary strings
: "YaaaY" and "123YaaaY"
: I'm having trouble matching only if "aaa" exist between Ys without
: having 123 preceeding it.
:
: example:
: "YaaaY" --> pass
: " YaaaY " --> pass
: "XXXYaaaYXXX" --> pass
: "alskdjlkajf 123YaaaY laskdjf" --> fail

/(?<!123)YaaaY/

: my one liner didn't work
: $x =~ s/(!\123)(Y)(a{3})(Y)/$1$2$3$4/g;
: the ! doesn't seem to negate it.

What did you read in perlre that made you believe it would?

: for an extra bonus, how about if I wanted "aaa" to go between "[" and
: "]"? or ( and )?

See perlfaq6, "Can I use Perl regular expressions to match balanced text?"
 
K

Ken Sington

Jay said:
.......


/(?<!123)YaaaY/

: my one liner didn't work
: $x =~ s/(!\123)(Y)(a{3})(Y)/$1$2$3$4/g;
: the ! doesn't seem to negate it.

What did you read in perlre that made you believe it would?
isn't ! suppose to negate (and [^] of course)??
I thought I saw it used in regex somewhere, but I can't find it again.
Has anyone seen such a thing?
Or am I having one of my many delusions?
 
F

Fatted

Ken said:
i have these arbitary strings
"YaaaY" and "123YaaaY"
I'm having trouble matching only if "aaa" exist between Ys without
having 123 preceeding it.

example:
"YaaaY" --> pass
" YaaaY " --> pass
"XXXYaaaYXXX" --> pass
"alskdjlkajf 123YaaaY laskdjf" --> fail

I think this will take care of the simple match:

my $string = '667999123YaaaY44adadfYaaaY';
while($string =~ /(123|)YaaaY/g)
{
unless($1 eq '123')
{
print "Found\n";
}
}
for an extra bonus, how about if I wanted "aaa" to go between "[" and
"]"? or ( and )?

If you mean That match string then becomes Y(aaa)Y or Y[aaa]Y, then:

my $string = '667999123YaaaY44adadfYaaaY Y[aaa]Y';
$string .= ' adagf123Y(aaa)Yblah33Y((aaa))YsausagesY(aaa)Y';

while($string =~ /(123|)Y([\(|\[]?)aaa([\)|\]]?)Y/g)
{
unless($1 eq '123')
{
if($2 eq '' && $3 eq '')
{
print "Found\n";
}
elsif($2 eq '(' && $3 eq ')')
{
print "Found\n";
}
elsif($2 eq '[' && $3 eq ']')
{
print "Found\n";
}
}
}

seems to work (matches 3 times).
 
B

Ben Morrow

Quoth Ken Sington said:
isn't ! suppose to negate??

Not in regexen.
I thought I saw it used in regex somewhere, but I can't find it again.
Has anyone seen such a thing?
No.

Or am I having one of my many delusions?

Apparently.

Ben
 
T

Tad McClellan

Ken Sington said:
Jay Tilton wrote:

now to find out what ?<! means...


It is actually a 4-character opening token (?<! and a 1-char close token )

See "zero-width negative look-behind assertion" in perlre.pod.
 
K

Ken Sington

Tad said:
It is actually a 4-character opening token (?<! and a 1-char close token )

See "zero-width negative look-behind assertion" in perlre.pod.

I noticed in:
s/(?<!123)(1)(2)(3)/$1$2$3/
the first set of () dosen't seem to count.
I would have expected a forth one to be $4
what happened to $4? or should I say -$1?
 
A

Anno Siegel

Ken Sington said:
I noticed in:
s/(?<!123)(1)(2)(3)/$1$2$3/
the first set of () dosen't seem to count.
I would have expected a forth one to be $4
what happened to $4? or should I say -$1?

Experimentation is fine, but you should complement it by Looking Things
Up. The behavior you noted is described in perlre under the heading
"Extended Patterns".

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,413
Latest member
KeiraLight

Latest Threads

Top