Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to replace only if not preceded by

Tags:

regex

Apologies for my blatant lack of knowledge on regular expressions, I understand lots of questions crop up here on them, but try as I might for hours on end I cannot figure this out. Basically what I am trying to do is replace all occurrences of 6 digit numbers (with or without hyphens) in a string. However I do not wish to replace numbers if preceded by certain words.

This Regular expression to match a line that doesn't contain a word? solution comes close to what I am looking for but I cannot seem to use it in a way that works for my requirements.

What I need is as follows: For the string:

"User paid £43 on 23/05/14 to account 123456 with cheque 123456 transaction: 123456."

I wish to only replace the 6 digit number not preceded by "cheque", or "transaction:". What I have been trying is as follows:

\b[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b 

(This replaces all 6 digit numbers)

Using this How do you replace a match, using regex, only if it is not preceded by a given character? answer, I tried

(^cheque\s[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b) 

(Please note I am trying first for one of the words I wish to escape and will then include the others.) This does not replace any of the 6 digit numbers.

Through trial and error I have found

(cheque\s+[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b) 

will replace the word cheque followed by a 6 digit number so I am getting there - but I need to negate this (and transaction followed by 6 digit number) and replace instead the 6 digit number not preceded by these words.

This How to negate the whole regex? answer is helpful on figuring out how to negate the expression but try as I might, I cannot find how to make it work for my situation. I tried

^(?!(?:((transaction\s+[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b) )|((cheque\s+[0-9]{2}-?[0-9]{2}-?[0-9]{2}\b) ))$).*$

but this replaced the whole string!

Any help on this is greatly appreciated.

Thanking you.

like image 899
Misemefein Avatar asked Mar 09 '15 16:03

Misemefein


People also ask

How do you match a space in regex?

If you're looking for a space, that would be " " (one space). If you're looking for one or more, it's " *" (that's two spaces and an asterisk) or " +" (one space and a plus).

What is string regex?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

What is regular expression with example?

Solution: As we know, any number of a's means a* any number of b's means b*, any number of c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So the regular expression could be: R = a* b* c*


1 Answers

Try this:

(?<!(?:cheque|transaction:)\s*)\d{2}-?\d{2}-?\d{2}\b

Explanation:

  • (?<! ... ) Negative lookbehind assertion (match anything not preceded by this)
  • (?:cheque|transaction:)\s* non-capturing group "cheque" or "transaction:" followed by any number of spaces
  • \d{2}-?\d{2}-?\d{2}\b Six-digit number possibly hyphenated, ending in word boundary
like image 106
tzaman Avatar answered Sep 18 '22 12:09

tzaman