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.