LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   regular expressions and back references within [] lists (https://www.linuxquestions.org/questions/programming-9/regular-expressions-and-back-references-within-%5B%5D-lists-127299/)

Tramontane 12-19-2003 12:22 PM

regular expressions and back references within [] lists
 
It seems like a simple problem at first.

Let's start with an example:

using the data set
Code:

war
wee
wet
woo

I would like to be able to match the words war and wet while excluding wee and woo.

I can match wee and woo with this expression:
Code:

"^[a-z]([a-z])\1$"
Is there a way to use "back references" within [] lists?

The expression:
Code:

"^[a-z]([a-z])[\1]$"
does not produce the same results as the previous expression because instead of resolving the '\1' to an 'e' or 'o', it searches for the '\\' and '1' characters.

The reason this is a problem is that I thought that I would be able to write an expression like this:
Code:

"^[a-z]([a-z])[^\1]$"
So is there any way to resolve a back reference within [] lists?


I've been trying to think back to my CS classes to remember if it's even possible for REs to do this sort of thing.

vladkrack 12-19-2003 01:53 PM

The usual reference is [^], but in this case will not work, so is easier to use an work around, like in egrep use the option -v to reverse the result of this expression...


All times are GMT -5. The time now is 01:02 AM.