Reputation: 153
I am needing a bit of assistance in a regular expression I am trying to create, I am usually pretty good with these but this one has me stuck...
I am needing to replace the number 9 in strings on the fly with \d (creating a new regular expression). These are some example strings that can be expected:
X(29)
9(5)
99
X(29)V999S
Now before you answer too quickly, we must not touch the numbers in the brackets... I have looked at a couple of other answers on here and there is probably something in this solution, but I cannot seem to adapt it - Regex for splitting a string using space when not surrounded by single or double quotes
So far I have come up with
line.replaceAll("[^(\\d)]??9[^)]??", "\\d");
which gives me
X(2\d)
\d(5)
\d\d
X(2\d)V\d\d\dS
Anyone have any thoughts.
Upvotes: 2
Views: 218
Reputation: 210
given that your rules are pretty straight-forward but just difficult to implement with regular expressions, why not do-it-yourself? Iterate over the string and create a new string keeping track of the state (in brackets or out).
Barry
Upvotes: 2