0

I'm having an issue with a Regex. I basically need to split a string with Chr(1) as a delimiter and each group is formatted as key=value (where key is numeric). The beginning of the string is a 3-char code and is not gathered with the Regex. Example:

031|0=2013/12/04 00:03:35|400=lr5ulz1jxg8ss|3=SFE|4=2$1NNR|6=1

So I used the following Regexp:

Private Shared _RegexDeserialize As New Regex(String.Format("{0}(\d+)=([^${0}]*)", Chr(1)))

But when there is a dollar ($) in the value part, the rest is not matched.

Then I used the following:

Private Shared _RegexDeserialize As New Regex(String.Format("{0}(\d+)=([^{0}]*)", Chr(1)))

I don't understand why my previous Regex doesn't work properly and why VB.Net is matching the end-of-string delimiter ($) to the dollar ($) of the string. If I wanted to match to a dollar, I should have escaped it: \$

Any help will be appreciated. Thanks very much.

3
  • 2
    If I wanted to match to a dollar, I should have escaped it: \$. Yes you answered yourself. Commented Feb 4, 2014 at 16:21
  • 2
    All characters inside a character class are treated as literal characters and not as regex meta characters, except the characters ^ and - which take different meanings depending on their position in the character class. Commented Feb 4, 2014 at 16:46
  • i think the correct pattern should be: "(\d+)=([^|]*)" in your example takes 5 matches and for each two groups before and after "=" Commented Feb 4, 2014 at 16:50

2 Answers 2

3

[...] is a character class. Normally, $ (along with many other characters such as *+?)[.(|) are metacharacters. However, when they are inside a character class, they become literal characters. This means that they lose their special meaning, so a $ inside a character class matches $, not and end-of-string. Instead of $, you can use \Z which is the equivalent, but still works inside character classes.

I'm not sure exactly what you are trying to accomplish, but I think the regex you want is

\d+=[^|]+

Demonstration here: http://regex101.com/r/rI4tM3

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your help. I didn't know this specificity.
Are you saying [\Z] matches the end of a string, just like \Z? That's not true. It's just a syntax error.
2

Well, your $ is inside of [^...]. It can't mean end-of-string in that location, so it thinks you mean a literal $.

The reason this "usually" works for you is because you don't actually need the end-of-string at all :)

"{0}(\d+)=([^{0}]*)"

The * is greedy, it will eat up as many characters as it can, so everything works just fine :)

1 Comment

Thanks a lot for your help and to confirm me that the fix-version will work properly. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.