How to put an AND in a regex?

How to put an AND in a regex?

am 13.10.2011 12:33:17 von hamann

=0A=
Hi,=0A=
=0A=
I am trying to write a regex that should only match when certain patterns a=
re not present, e.g. when a line does not start with either a digit or ALL-=
CAPS text. I figured I could use negative look-aheads for this.=0A=
=0A=
I can write it as:=0A=
=0A=
if (/^(?![[:upper:]][[:upper:]])/) {=0A=
if (/^(?!\d)/) {=0A=
s/^//;=0A=
}=0A=
else {=0A=
}=0A=
}=0A=
else {=0A=
}=0A=
=0A=
However, I was wondering whether there was a way of writing this as a singl=
e if loop, because there are much more than two situations that should not =
be matched.=0A=
=0A=
I tried to write it as:=0A=
=0A=
if (/^(?![[:upper:]][[:upper:]])|^(?!\d)/) {=0A=
s/^//;=0A=
}=0A=
else {=0A=
}=0A=
=0A=
but this means if one option is not matched the other one is matched, which=
is not what I want. So I need something that does the equivalent of "Don't=
match this AND don't match this". Is this possible in a if loop, or should=
I use something else?=0A=
=0A=
Thanks,=0A=
=0A=
Regards,=0A=
Thomas Hamann=0A=

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to put an AND in a regex?

am 13.10.2011 14:10:45 von Igor Dovgiy

--0016e6de14d5cb807104af2d070f
Content-Type: text/plain; charset=ISO-8859-1

Hmm, probably you should. To use two of them in AND combination, just... use
two of them. )

/^(?![[:upper:]][[:upper:]])(?!\d)/

And it gets even better: you may mix any number of look-aheads in a single
regex this way. )

-- iD

2011/10/13 Hamann, T.D. (Thomas)

>
> Hi,
>
> I am trying to write a regex that should only match when certain patterns
> are not present, e.g. when a line does not start with either a digit or
> ALL-CAPS text. I figured I could use negative look-aheads for this.
>
> I can write it as:
>
> if (/^(?![[:upper:]][[:upper:]])/) {
> if (/^(?!\d)/) {
> s/^//;
> }
> else {
> }
> }
> else {
> }
>
> However, I was wondering whether there was a way of writing this as a
> single if loop, because there are much more than two situations that should
> not be matched.
>
> I tried to write it as:
>
> if (/^(?![[:upper:]][[:upper:]])|^(?!\d)/) {
> s/^//;
> }
> else {
> }
>
> but this means if one option is not matched the other one is matched, which
> is not what I want. So I need something that does the equivalent of "Don't
> match this AND don't match this". Is this possible in a if loop, or should I
> use something else?
>
> Thanks,
>
> Regards,
> Thomas Hamann
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

--0016e6de14d5cb807104af2d070f--

RE: How to put an AND in a regex?

am 13.10.2011 14:31:25 von hamann

Thanks!=0A=
=0A=
That was really simple (so simple I did not think about it ;)=0A=
=0A=
Thomas=0A=
=0A=
________________________________________=0A=
Van: Igor Dovgiy [ivd.privat@gmail.com]=0A=
Verzonden: donderdag 13 oktober 2011 14:10=0A=
Aan: beginners@perl.org=0A=
Onderwerp: Re: How to put an AND in a regex?=0A=
=0A=
Hmm, probably you should. To use two of them in AND combination, just... us=
e=0A=
two of them. )=0A=
=0A=
/^(?![[:upper:]][[:upper:]])(?!\d)/=0A=
=0A=
And it gets even better: you may mix any number of look-aheads in a single=
=0A=
regex this way. )=0A=
=0A=
-- iD=0A=
=0A=
2011/10/13 Hamann, T.D. (Thomas) =0A=
=0A=
>=0A=
> Hi,=0A=
>=0A=
> I am trying to write a regex that should only match when certain patterns=
=0A=
> are not present, e.g. when a line does not start with either a digit or=
=0A=
> ALL-CAPS text. I figured I could use negative look-aheads for this.=0A=
>=0A=
> I can write it as:=0A=
>=0A=
> if (/^(?![[:upper:]][[:upper:]])/) {=0A=
> if (/^(?!\d)/) {=0A=
> s/^//;=0A=
> }=0A=
> else {=0A=
> }=0A=
> }=0A=
> else {=0A=
> }=0A=
>=0A=
> However, I was wondering whether there was a way of writing this as a=0A=
> single if loop, because there are much more than two situations that shou=
ld=0A=
> not be matched.=0A=
>=0A=
> I tried to write it as:=0A=
>=0A=
> if (/^(?![[:upper:]][[:upper:]])|^(?!\d)/) {=0A=
> s/^//;=0A=
> }=0A=
> else {=0A=
> }=0A=
>=0A=
> but this means if one option is not matched the other one is matched, whi=
ch=0A=
> is not what I want. So I need something that does the equivalent of "Don'=
t=0A=
> match this AND don't match this". Is this possible in a if loop, or shoul=
d I=0A=
> use something else?=0A=
>=0A=
> Thanks,=0A=
>=0A=
> Regards,=0A=
> Thomas Hamann=0A=
>=0A=
> --=0A=
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org=0A=
> For additional commands, e-mail: beginners-help@perl.org=0A=
> http://learn.perl.org/=0A=
>=0A=
>=0A=
>=0A=

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to put an AND in a regex?

am 13.10.2011 16:14:13 von Peter Scott

On Thu, 13 Oct 2011 10:33:17 +0000, Hamann, T.D. (Thomas) wrote:
> I am trying to write a regex that should only match when certain
> patterns are not present, e.g. when a line does not start with either a
> digit or ALL-CAPS text. I figured I could use negative look-aheads for
> this.
>=20
> I can write it as:
>=20
> if (/^(?![[:upper:]][[:upper:]])/) {
> if (/^(?!\d)/) {
> s/^//;
> }
> else {
> }
> }
> else {
> }
>=20
> However, I was wondering whether there was a way of writing this as a
> single if loop, because there are much more than two situations that
> should not be matched.
>=20
> I tried to write it as:
>=20
> if (/^(?![[:upper:]][[:upper:]])|^(?!\d)/) { s/^//;
> }
> else {
> }
>=20
> but this means if one option is not matched the other one is matched,
> which is not what I want. So I need something that does the equivalent
> of "Don't match this AND don't match this". Is this possible in a if
> loop, or should I use something else?

Am I missing something about why this approach won't work for you:

if ( ! /\A[[:upper:]][[:upper:]]/ && ! /\A[[:digit:]]/ )=20

Seems easier to understand.

--=20
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/certificates/perl-programming.p hp

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to put an AND in a regex?

am 13.10.2011 18:36:10 von John Delacour

At 10:33 +0000 13/10/11, Hamann, T.D. (Thomas) wrote:

>...So I need something that does the equivalent of "Don't match this
>AND don't match this". Is this possible in a if loop, or should I
>use something else?

for (qw (bcd 1num ASDSF two)){
print "$_\n" if !/^[0-9]/ and !/^[A-Z]+/;
}

JD


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to put an AND in a regex?

am 14.10.2011 05:29:25 von Rob Dixon

On 13/10/2011 11:33, Hamann, T.D. (Thomas) wrote:
>
> Hi,
>
> I am trying to write a regex that should only match when certain
> patterns are not present, e.g. when a line does not start with either
> a digit or ALL-CAPS text. I figured I could use negative look-aheads
> forthis.
>
> I can write it as:
>
> if (/^(?![[:upper:]][[:upper:]])/) {
> if (/^(?!\d)/) {
> s/^//;
> }
> else {
> }
> }
> else {
> }
>
> However, I was wondering whether there was a way of writing this as
> a single if loop, because there are much more than two situations
> that should not be matched.
>
> I tried to write it as:
>
> if (/^(?![[:upper:]][[:upper:]])|^(?!\d)/) {
> s/^//;
> }
> else {
> }
>
> but this means if one option is not matched the other one is
> matched, which is not what I want. So I need something that does the
> equivalent of "Don't match this AND don't match this". Is this
> possible in a if loop, or should I use something else?

How about

unless (/^[[:upper]]{2}/ or /^\d/) {
s/^//;
}

Which seems to me to be much clearer in purpose than anything else that
has been posted.

HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: How to put an AND in a regex?

am 14.10.2011 08:51:51 von derykus

On Oct 13, 3:33=A0am, ham...@nhn.leidenuniv.nl ("Hamann, T.D. (Thomas)")
wrote:

>
> I am trying to write a regex that should only match when certain patterns=
are not present, e.g. when a line does not start with either a digit or AL=
L-CAPS text. I figured I could use negative look-aheads for this.
>
> I can write it as:
>
> if (/^(?![[:upper:]][[:upper:]])/) {
> =A0 =A0 if (/^(?!\d)/) {
> =A0 =A0 s/^//;
> =A0 =A0 }
> =A0 =A0 else {
> =A0 =A0 }
>
> }
> else {
> }
>
> However, I was wondering whether there was a way of writing this as a sin=
gle if loop, because there are much more than two situations that should no=
t be matched.
>
> I tried to write it as:
>
> if (/^(?![[:upper:]][[:upper:]])|^(?!\d)/) {
> s/^//;
>
> }
> else {
> }
>
> but this means if one option is not matched the other one is matched, whi=
ch is not what I want. So I need something that does the equivalent of "Don=
't match this AND don't match this". Is this possible in a if loop, or shou=
ld I use something else?

Yet another way:

unless ( /^ (?: [[:upper:]]{2} | \d ) /x ) {
...
}

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/