Regular Expression match to stop at a 2 characters combination or End of String

theskiguy

Member
Joined
Mar 18, 2013
Messages
8
Programming Experience
3-5
Hi Everyone:

Hopefully I'm posting this in the right section. I looked for a separate REGEX section but didn't see one. I need some help with a REGEX match. I am trying to match the following with this regex and return everything between the $ and the ") or end of line. The REGEX below works but stops after either a " or a ). I would like it to only stop after a combination of both or the end of the line.. This way it won't stop at the (8) on the 7th test below

\$\s?([^"\)]*)



N6942 MSG ("$ PROGRAM STOP")
N16 MSG("$SETUP PER SETUP SHEET")
MSG("$SETUP PER SETUP SHEET")
N16 ;$SETUP PER SETUP SHEET
;$SETUP PER SETUP SHEET
;$ SETUP PER SETUP SHEET
;$SETUP PER SETUP SHEET (8) TIMES
; THIS SHOULD NOT MATCH

Thanks,
 
Solution
I would just write the whole pattern and name the group of interest, then get the Match.Groups(name).Value

If you're matching this as a multiline text (RegexOptions.Multiline) to exclude newline chars:
VB.NET:
Dim pattern = "\$(?<m>.+?)(\""\)|\r)"
If you're matching this line by line:
VB.NET:
Dim pattern = "\$(?<m>.+?)(\""\)|$)"
Groups("m") has the matching strings.
I would just write the whole pattern and name the group of interest, then get the Match.Groups(name).Value

If you're matching this as a multiline text (RegexOptions.Multiline) to exclude newline chars:
VB.NET:
Dim pattern = "\$(?<m>.+?)(\""\)|\r)"
If you're matching this line by line:
VB.NET:
Dim pattern = "\$(?<m>.+?)(\""\)|$)"
Groups("m") has the matching strings.
 
Solution
I would just write the whole pattern and name the group of interest, then get the Match.Groups(name).Value

If you're matching this as a multiline text (RegexOptions.Multiline) to exclude newline chars:
VB.NET:
Dim pattern = "\$(?<m>.+?)(\""\)|\r)"
If you're matching this line by line:
VB.NET:
Dim pattern = "\$(?<m>.+?)(\""\)|$)"
Groups("m") has the matching strings.

I am doing it line by line. Unfortunately this pattern also includes the ") at the trailing end of my first 3 tests which I don't want. Any way to remove those? Also, I have never used a named match before. If I use a MatchedCollection in VB.net, is this still considered group #1?

mc = Regex.Matches(text, expr)
Result = mc(0).Groups(1).Value
 
You're grabbing the wrong group, get the named group "m".
 
1738353239332.png


Did I do something wrong? For me Group M still shows the trailing ")
 
In that screenshot it appears you have multiple matches in a single line. Still, I can't reproduce your "m" result with the pattern I posted.
 
In case you mangled something up, in the pattern there are three parts:

\$ - this is the escaped $ char

(?<m>.+?) this is the named "m" group (?<m> ... ) which matches any char lazy repeated 1 or more times .+?

(\"\)|$) - this group is an OR expression, either ") with both chars regex escaped to \"\) OR end of line $

Notice that in a VB string the " char must be escaped to "", so inside the VB string literal it is written \""\)
If you wanted a VB string that said 'Hello "old" friend' it would be Dim example = "Hello ""old"" friend"
 
Thanks for the explanation. Are you using the Regex101 site for your testing? I figured out how to save the custom regex to the Regex101 site. Could you try the link to see what I'm seeing?

 
You have one too many " chars in part 3 of expression. That "" was an escape for VB code, remember?

And while we're at it, in expression part 3 it is not necessary to regex escape the " as \", I was mixing with C# string literals. I doesn't matter for the result, but part 3 in regex can be written ("\)|$) and (""\)|$) in a VB string literal.

I'm sometimes using online regex sites for testing and trying out patterns, but main focus here is VB code.
 
I got it to work now in Regex101 but wanted to clarify what I think you are saying

When I use it in Regex101, I should use this:
\$(?<m>.+?)(\"\)|$)

when I use it in VB, I should use this because it is a string and the "" would mess up Vb's interpretation of a string.
"\$(?<m>.+?)(\""\)|$)"

Thanks for being patient with this.
 
Regex: \$(?<m>.+?)("\)|$)
VB string: "\$(?<m>.+?)(""\)|$)"
 
Back
Top