tags:

views:

42

answers:

3

I am trying to extract a section of text that looks something like this:

Thing 2A blah blah Thing 2A blah blah Thing 3

Where the "3" above could actually be ANY single digit. The code I have that doesn't work is:

((Thing\s2A).+?(Thing\s\d))

Since the 3 could be any single digit, I cannot simply replace the "\d" with "3". I tried the following code, but it doesn't work either:

((Thing\s2A).+?(Thing\s\d[^A]))

Please help!

+1  A: 

Is this what you're trying to do?

((Thing\s2A).+?(Thing\s[0-9]))

Edit: Oh I get it, you want a digit not followed by an A. Use a forward lookahead

((Thing\s2A).+?(Thing\s[0-9](?!A))

That's assuming you want it not followed by an A. Replace the A with whatever you DON'T want to follow the digit

((Thing\s2A).+?(Thing\s[0-9](?![A-Za-Z]))

Or if you know what you expect after the digit, you can add it. For example

((Thing\s2A).+?(Thing\s[0-9][\s$])

That will match Thing 3 followed by a space, tab, or a newline

Falmarri
This was a perfect solution and very thorough! Thanks!
dandyjuan
A: 

Do you mean this?

(((Thing\s2A).+)?(Thing\s\d))

bosmacs
A: 

It is not very clear what you want exactly...

The forward lookahead of @Falmarri might help.

If you want the last Thing followed by a digit you could use

 ((Thing\s2A).+(Thing\s\d))

(removing the ? after the + indicates it uses the default 'greedy' matching and it will eat all till the last matching Thing foolowed by a digit)

If the last thing is always the last on the line then this might help

((Thing\s2A).+(Thing\s\d))\s*$

The \s* is to throw away some remaining spaces at the end of the line.

Peter Tillemans