rbobbington
rbobbington

Reputation: 123

Discard regex match if certain word appears immediately before

I'm completely unexperienced with regex, so I apologise if this is a simple question, but I've been unable to find any answers anywhere. I'm trying to match strings of the form [some numbers] [some letters] such as 123 apples but I don't want to match them when they are preceeded by the word about (e.g. about 10 apples should not match).

I've tried using (?<!about )(\d[,\.\d]*) ([a-zA-Z]+) but that obviously doesn't work as it just moves the match on somewhat (such that about 12 apples matches as 2 apples). What can I do to solve this?

Upvotes: 2

Views: 1000

Answers (2)

agent-j
agent-j

Reputation: 27943

You were so close: (?<!about \d*)(\d[,\.\d]*). Add \d* to your negative look behind.

Upvotes: 0

robert
robert

Reputation: 34458

I've had this problem before. Try this:

(?<!about )(\b\d[,\.\d]*\b) ([a-zA-Z]+)

The \b matches a zero-character word boundary (space followed by alphanumeric or vice-versa). It will refuse to match "about 123 apples" as "23 apples", because there's no word boundary immediately before the "2".

Upvotes: 1

Related Questions