regexp question

E

Eric Gibson

Hey all,

I'm trying to do a regexp that will gracefully extract the fields (fn)
from a string in this format:

(f1, f2, f3),(f1, f2, f3),(f1, f2, f3),...,(f1, f2, f3)

Is there a way I could grab each (f1, f2, f3) and then iterate through
them gracefully. I'm using Split(str, ",\)") , but then I have to remove
the ('s and )'s from the beginning and end of the first and last () set.
It's not a big deal, it's just ugly.

Can't I do a regexp like "\(.*\)", and iterate through every match in a
string of indeterminate length?

Thanks,

Eric
 
C

Chris R. Timmons

Hey all,

I'm trying to do a regexp that will gracefully extract the
fields (fn)
from a string in this format:

(f1, f2, f3),(f1, f2, f3),(f1, f2, f3),...,(f1, f2, f3)

Is there a way I could grab each (f1, f2, f3) and then
iterate through
them gracefully. I'm using Split(str, ",\)") , but then I have
to remove the ('s and )'s from the beginning and end of the
first and last () set. It's not a big deal, it's just ugly.

Can't I do a regexp like "\(.*\)", and iterate through every
match in a
string of indeterminate length?

Eric,

Yes you can. Use \((?<content>.*?)\) to get 1-N capture groups named
"content". Each group will contain f1, f2, f3 (without the
parentheses).
 
E

Eric Gibson

Chris said:
[snip]

Yes you can. Use \((?<content>.*?)\) to get 1-N capture groups named
"content". Each group will contain f1, f2, f3 (without the
parentheses).

Perfect, I eventually came up with (well, sort of) this:


Dim inputStr As String = "(f1, f2, f3), (f4, f5, f6)"
Dim inputStrFlds As String()
Dim msg As String

Dim Regexp as Regex = New Regex("\((?<content>.*,.*,.*?)\)",
RegexOptions.IgnoreCase)
Dim objMatch as Match

If inputStr.Length > 0 Then

'Create a Match object instance / iterate through the MatchCollection

For Each objMatch in Regexp.Matches(inputStr)

inputStrFlds = Split(objMatch.Groups("content").Value, ",")
If UBound(inputStrFlds) <> 0 Then
Response.Write(inputStrFlds(0) & " and " & inputStrFlds(1) & _
" and " & invalidFlds(2) & "<br>")
End If
Next

End If

Thanks!

Eric
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top