Regex Expression to split on a quote

L

lgbjr

Hello All,

I have the following type of string: "X:Y\Z.exe" "123"

What I need is an array of strings with the information from within each set
of quotes. I was trying to use a Regex.Split, but I can't figure out how to
escape a quote. The closest I got was Regex.split(String,"(\"")"), which
gives me an array of strings:

"X:Y\Z.exe
"
"123

I thought that using (\") would be what I needed (escaping a single quote),
but that produces a syntax error.

Can someone tell me the correct syntax for a Regex expression to split on a
quote. Or, if there's a better way to do this, I'm open to suggestions.

TIA,

Lee
 
P

Peter Huang [MSFT]

Hi

In VB syntax, the "" is the escape character for ".

Here is a module for your reference.
Module Module1
Sub Main()
Dim str As String = "asdfsadf""fasdfasdf""fasdfasdf"
Dim strs() As String = Split(str, """")
For Each s As String In strs
Console.WriteLine(s)
Next
End Sub
End Module


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jay B. Harlow [MVP - Outlook]

Lee,
What are you expecting as output?

Reading "within each set of quotes" it sounds like you expect a 2 element
array with:

(0) = "X:Y\Z.exe"
(1) = "123"

I would use a pattern such as "(?<value>.*?)" (including quotes), and use a
loop to find each match.

Something like:

Dim input As String = """X:Y\Z.exe"" ""123"""
Dim pattern As String = """(?<value>.*?)"""
Static parser As New System.Text.RegularExpressions.Regex(pattern,
RegularExpressions.RegexOptions.Compiled)

Dim list As New ArrayList

Dim match As match = parser.Match(input)
Do While match.Success
list.Add(match.Groups("value").Value)
match = match.NextMatch()
Loop


Expresso & RegEx Workbench both have wizards of varying degrees to help you
build your expression, plus they allow you to test your expressions, also
the analyzer/interpreter in each is rather handy.

Expresso:
http://www.ultrapico.com/Expresso.htm

RegEx Workbench:
http://www.gotdotnet.com/Community/...pleGuid=c712f2df-b026-4d58-8961-4ee2729d7322A

tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/d...l/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top