I'm really not good with Regex and have been messing about to achieve the following all morning:
I want to find unicode characters ie "\00026" in an SQL string before saving to the database and escape the "\", by replacing it with "\" unless it already has two "\" characters.
\\(?=[0])(?<![\\])
Is what I have written, which as I understand it does:
find the "\" character, positive look ahead for a "0", and look behind to check it isn't preceded by a "\"
But it's not working, so clearly I have misunderstood!
I can shorten it to \\(?=[0])
But then I get the "\" before the 0, even if it is preceded by another "\"
So how do I do:
Replace("\00026", "regex", "\\") to get "\\00026"
AND ensure that
Replace("\\00026", "regex", "\\") also gives "\\00026"
All help much appreciated!
EDIT:
This must parse an entire string and replace all occurrences, not just the first as well - just to be clear. Also I am using VB.net if it makes much difference.
Let me explain why your regex does not work.
\\
- Matches\
(?=[0])
- Checks (not matches) if the next character is0
(?<![\\])
- Checks (but not matches) if the preceding character (that is\
) is not\
.The last condition will always fail the match, as
\
is\
. So, not much sense, right?If you want to match
/
in/000xx
whole strings (e.g. separated with spaces), wherex
is any digit, you can useSee demo (go to Context tab)
To match the string even in context like
w/00023
, you can remove\B
:If you do not care about
0
s, but just any digits:And in case you have
\
(not/
), just replace/
with\\
in the above regular expressions.