[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp to match strings that _don't_ being with a string

Wes Gamble

3/28/2006 6:25:00 PM

I would like to write a regexp that will match a string that does NOT
start with a specified set of characters.

For example,

Given:

xyz123
asldfhsl
xyk2345

and assume that I want to see only strings that don't start with "xyz"
(so in this case, the last 2 in the list).

I tried /^[^(xyz)]/ but I don't trust it. I don't think the grouping
will take inside the character class.

Do I need a negative lookahead assertion?

Thanks,
Wes

--
Posted via http://www.ruby-....


13 Answers

Andrew Johnson

3/28/2006 6:34:00 PM

0

On Wed, 29 Mar 2006 03:24:36 +0900, Wes Gamble <weyus@att.net> wrote:

> I tried /^[^(xyz)]/ but I don't trust it. I don't think the grouping
> will take inside the character class.

The [^(xyz)] creates a negative character class, so your regex would
match any string that started with a character not in the given set.
Not what you really want

> Do I need a negative lookahead assertion?

That would be a simple solution: /^(?!xyz)/ which will match when
the beginning of the line/string is not followed by 'xyz'.

andrew

--
Andrew L. Johnson http://www.s...
What have you done to the cat? It looks half-dead.
-- Schroedinger's wife

Chris Alfeld

3/28/2006 6:40:00 PM

0

What about:

! s =~ /^xyz/

There is a good discussion of doing exactly this with negative
look-aheads in 'man perlre'. It's... ugly.


Wes Gamble

3/28/2006 6:44:00 PM

0

In my example, won't /^(?!xyz)/ also match

29384723xyz02342

which is a little more than I want?

WG

Andrew Johnson wrote:
> On Wed, 29 Mar 2006 03:24:36 +0900, Wes Gamble <weyus@att.net> wrote:
>
>> I tried /^[^(xyz)]/ but I don't trust it. I don't think the grouping
>> will take inside the character class.
>
> The [^(xyz)] creates a negative character class, so your regex would
> match any string that started with a character not in the given set.
> Not what you really want
>
>> Do I need a negative lookahead assertion?
>
> That would be a simple solution: /^(?!xyz)/ which will match when
> the beginning of the line/string is not followed by 'xyz'.
>
> andrew


--
Posted via http://www.ruby-....


James Gray

3/28/2006 6:46:00 PM

0

On Mar 28, 2006, at 12:40 PM, Chris Alfeld wrote:

> ! s =~ /^xyz/

Ruby has a doesn't match operator for this:

s !~ /^xyz/

James Edward Gray II



Andrew Johnson

3/28/2006 6:54:00 PM

0

On Wed, 29 Mar 2006 03:43:51 +0900, Wes Gamble <weyus@att.net> wrote:
> In my example, won't /^(?!xyz)/ also match
>
> 29384723xyz02342
>
> which is a little more than I want?

Uhm, maybe I've misunderstood (wouldn't be the first time) -- I thought you
wanted to match strings that did not begin with 'xyz' ... and as far as I
can tell, "29384723xyz02342" does not start with 'xyz'.

while DATA.gets
print if ~/^(?!xyz)/
end
__END__
xyefoo
xyzpfoo
asdfsdf
1230xyzasdf

produces:

xyefoo
asdfsdf
1230xyzasdf


puzzled,
andrew

--
Andrew L. Johnson http://www.s...
People seem not to see that their opinion of the world is
also a confession of character.
-- Ralph Waldo Emerson; The Conduct of Life, 1860

Wes Gamble

3/28/2006 6:57:00 PM

0

Hey that's too easy!!!

Thanks James.

I'm liking Ruby more and more :).

Wes

James Gray wrote:
> On Mar 28, 2006, at 12:40 PM, Chris Alfeld wrote:
>
>> ! s =~ /^xyz/
>
> Ruby has a doesn't match operator for this:
>
> s !~ /^xyz/
>
> James Edward Gray II


--
Posted via http://www.ruby-....


Justin Collins

3/28/2006 7:04:00 PM

0

Wes Gamble wrote:
> Hey that's too easy!!!
>
> Thanks James.
>
> I'm liking Ruby more and more :).
>
> Wes
>
> James Gray wrote:
>
>> On Mar 28, 2006, at 12:40 PM, Chris Alfeld wrote:
>>
>>
>>> ! s =~ /^xyz/
>>>
>> Ruby has a doesn't match operator for this:
>>
>> s !~ /^xyz/
>>
>> James Edward Gray II
>>
Awesome.

-Justin


Wes Gamble

3/28/2006 7:30:00 PM

0

Andrew,

That works fine.

In actuality, I do need the ability to pass one regex to do the job into
another utility that will use it to operate on an array of strings.

So although !~ is cool, I really didn't want to have to iterate through
the strings myself.

Thanks,
Wes

Andrew Johnson wrote:
> On Wed, 29 Mar 2006 03:43:51 +0900, Wes Gamble <weyus@att.net> wrote:
>> In my example, won't /^(?!xyz)/ also match
>>
>> 29384723xyz02342
>>
>> which is a little more than I want?
>
> Uhm, maybe I've misunderstood (wouldn't be the first time) -- I thought
> you
> wanted to match strings that did not begin with 'xyz' ... and as far as
> I
> can tell, "29384723xyz02342" does not start with 'xyz'.
>
> while DATA.gets
> print if ~/^(?!xyz)/
> end
> __END__
> xyefoo
> xyzpfoo
> asdfsdf
> 1230xyzasdf
>
> produces:
>
> xyefoo
> asdfsdf
> 1230xyzasdf
>
>
> puzzled,
> andrew


--
Posted via http://www.ruby-....


Wes Gamble

3/28/2006 8:02:00 PM

0

I have a new wrinkle.

Now I want to match any line that doesn't have "xyz" or "abc" at the
beginning of the line.

Is there a way to "AND" together the input to the negative lookahead
assertion?

Wes

Wes Gamble wrote:
> Andrew,
>
> That works fine.
>
> In actuality, I do need the ability to pass one regex to do the job into
> another utility that will use it to operate on an array of strings.
>
> So although !~ is cool, I really didn't want to have to iterate through
> the strings myself.
>
> Thanks,
> Wes
>
> Andrew Johnson wrote:
>> On Wed, 29 Mar 2006 03:43:51 +0900, Wes Gamble <weyus@att.net> wrote:
>>> In my example, won't /^(?!xyz)/ also match
>>>
>>> 29384723xyz02342
>>>
>>> which is a little more than I want?
>>
>> Uhm, maybe I've misunderstood (wouldn't be the first time) -- I thought
>> you
>> wanted to match strings that did not begin with 'xyz' ... and as far as
>> I
>> can tell, "29384723xyz02342" does not start with 'xyz'.
>>
>> while DATA.gets
>> print if ~/^(?!xyz)/
>> end
>> __END__
>> xyefoo
>> xyzpfoo
>> asdfsdf
>> 1230xyzasdf
>>
>> produces:
>>
>> xyefoo
>> asdfsdf
>> 1230xyzasdf
>>
>>
>> puzzled,
>> andrew


--
Posted via http://www.ruby-....


Joel VanderWerf

3/28/2006 8:09:00 PM

0

Wes Gamble wrote:
> I have a new wrinkle.
>
> Now I want to match any line that doesn't have "xyz" or "abc" at the
> beginning of the line.
>
> Is there a way to "AND" together the input to the negative lookahead
> assertion?

For lookaheads, you can get AND by concatenating:


irb(main):001:0> /^(?!abc)(?!xyz)/ =~ "abc"
=> nil
irb(main):002:0> /^(?!abc)(?!xyz)/ =~ " abc"
=> 0
irb(main):003:0> /^(?!abc)(?!xyz)/ =~ "xyz"
=> nil
irb(main):004:0> /^(?!abc)(?!xyz)/ =~ " xyz"
=> 0

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407