a companion discussion area for blog.codinghorror.com

Excluding Matches With Regular Expressions


#1

Here's an interesting regex problem:


This is a companion discussion topic for the original blog entry at: http://www.codinghorror.com/blog/2005/10/excluding-matches-with-regular-expressions.html

#2

i faced a problem similar to this a few months ago where i had a template and i needed to look for anything in the string matching the template (ex. {something} look for something between the two braces). i searched high and low, to no avail, for a reg ex solution. it would seem as though a similar solution to the above problem could be used? maybe this could be done with a reg ex, i dunno (goes way past my knowledge of reg ex), in the end i had to write a string parsing algorithm.


#3

i needed to look for anything in the string matching the template (ex. {something} look for something between the two braces).

Hmm, something between the two braces:

"{[^}]+}"

Just watch out for a) non-escaped {} characters and b) line breaks inside the braces.


#4

I took advantage of this just last month. http://blog.eriklane.com/archive/2005/09/28/2105.aspx and I was pretty excited when I finally got it work. Woohoo!


#5

Old comments, I know. But if you just split on “fox” you’ll get an array of strings split on the occurrences of “fox”. For example, “foo fox bar” would return an array of two strings, “foo " and " bar”.

ORANGE!


#6

Your regex’s appear to use lookaheads/behinds in a different order from those on http://www.regular-expressions.info.

I thought lookaheads were positioned after the text whilst lookbehinds preceeded the text.


#8

This is great. I needed something to exclude a tag. I will test it to see if it works for my application.


#9

Very helpful when I needed to match anything but [Please Select]
^(?![Please Select]).*$


#10

echo $STRING | sed 's/\(...\)..\(..\)/\1\2/g'

The \( and \) are marks. By marking characters 1-3 and 6-7, you output using \1 and \2 for mark #1 and mark #2 respectively.


#11

What about a RegEx replace? You could do a replace on all of your matches with an empty string(or a “filtered” notification if you prefer) and achieve the same functionality.


#14

Good!
This was a HORROR for me, but you nailed it!