tags:

views:

70

answers:

2
+3  Q: 

regex no character

In this text:

warning here
there are several types of warnings
in this string warning.gif
at the end warning
end of line warning

I want to match every warning except warning.gif.

I cannot seem to get it to include the last one that has no character (control or otherwise) after it.

using warnings?[^\.] gets what I want except for the last warning (on the last line). I think because there is no character at all after. How can I get it to include that one?

+4  A: 

If you have negative lookaheads you can use that:

/warnings?(?!\.gif)/

This should work in Javascript.

Mark Byers
I'm working in javascript regex.
iamnotmad
Good news: Javascript has negative lookaheads.
Mark Byers
This is the winner! Thanks a lot! I changed the ".gif" to just "."
iamnotmad
Hi Mark, I'd like to add something to this. I want to NOT include warning where it is preceded by "0 " (zero space), but yes if any other number >0. Lastly do not include if there is any other character after warning or warnings except a space/end of line (or I think in my case nothing (no eol control chars). Can you help with that one?
iamnotmad
I got this to work with other chars besides . after. now it looks like: warnings?(?!\w) Now I just need to make it not include warning where there is a "0 " <zero space> in front, but any other number ok, or nothing.
iamnotmad
ok, here is what I have now:[^0]\swarnings?(?!\.|\w)which works great except it does not catch warning if first word on a line. it expects something before it.
iamnotmad
actually that will only miss if warning is the first word in the file, if it's at the beginning of any subsequent line it works.
iamnotmad
Try `(?:^|[^0])` instead of `[^0]`.
Mark Byers
+1  A: 

EDITED

I completely misread the question. My apologies.

Here's a corrected solution:

Alternatives can bind more tightly than the eof marker.


Example file:

warning here
there are several types of warnings
in this string warning.gif
at the end warning
end of line warning

Example script:

awk '/warnings?$|warnings?[[:space:]]/ { print }' /home/geocar/g.txt

Original (wrong) answer:

Use: /warnings?$/m - note that the "?" means that the "s" is optional

geocar
for some reason this did not catch the warning on the first line.
iamnotmad
@iamnotmad: You have something else wrong with your program. I've edited my post to include an example which you can try yourself.
geocar
This regex only matches if the word is at the end of the line, which is not always the case.
Alan Moore
the new one works. I'd just like to exclude warning where there is a "0 " <zero space> in front of it.
iamnotmad
@iamnotmad: Then use: `/^warnings?$|[[:space:]]warnings?$|^warnings?[[:space:]]|[[:space:]]warnings?[[:space:]]/`
geocar