Regular Expression question

J

Jerry J

I want to use the Regular Expressions MatchCollection to match multiple
patterns
in a string. I want to match the pattern that looks like "Word, Word".

Using the test code below I would want my match collection to contain
"Field1a, Field1b" and "Field3a, Field3b", but I can't get it to work. Can
someone show me the pattern I need to use?



Dim sString As String = """Field1a, Field1b"", Field2, ""Field3a, Field3b"",
Field4"

Dim sPattern As String = """[\S ]*,[\S ]*"""
Dim reg_exp As New Regex(sPattern)
Dim matches As MatchCollection = reg_exp.Matches(sString)
 
M

Martin Honnen

Jerry said:
I want to use the Regular Expressions MatchCollection to match multiple
patterns
in a string. I want to match the pattern that looks like "Word, Word".

Using the test code below I would want my match collection to contain
"Field1a, Field1b" and "Field3a, Field3b", but I can't get it to work. Can
someone show me the pattern I need to use?



Dim sString As String = """Field1a, Field1b"", Field2, ""Field3a, Field3b"",
Field4"

Dim sPattern As String = """[\S ]*,[\S ]*"""
Dim reg_exp As New Regex(sPattern)
Dim matches As MatchCollection = reg_exp.Matches(sString)

Dim sString As String = """Field1a, Field1b"", Field2,
""Field3a, Field3b"", Field4("")"
Dim matches As MatchCollection = _
Regex.Matches(sString, "(""(\w+)\w*, (\2)\w*"")")
For Each m As Match In matches
Console.WriteLine(m.Value)
Next
 
J

Jerry J

Martin, thanks for the info, however, I realized I was not correct with my
example it should have looked like this:

I want to use the Regular Expressions MatchCollection to match multiple
patterns
in a string. I want to match the pattern that looks like "Word, Word".

Using the test code below I would want my match collection to contain
"Field1a Field1b, Field1c" and "Field3a, Field3b", but I can't get it to
work. Can
someone show me the pattern I need to use? Is it possible?


Dim sString As String = """Field1a Field1b, Field1c"", Field2, ""Field3a,
Field3b"",
Field4"

Dim sPattern As String = """[\S ]*,[\S ]*"""
Dim reg_exp As New Regex(sPattern)
Dim matches As MatchCollection = reg_exp.Matches(sString)





--
Jerry J


:

Jerry said:
I want to use the Regular Expressions MatchCollection to match multiple
patterns
in a string. I want to match the pattern that looks like "Word, Word".

Using the test code below I would want my match collection to contain
"Field1a, Field1b" and "Field3a, Field3b", but I can't get it to work. Can
someone show me the pattern I need to use?



Dim sString As String = """Field1a, Field1b"", Field2, ""Field3a, Field3b"",
Field4"

Dim sPattern As String = """[\S ]*,[\S ]*"""
Dim reg_exp As New Regex(sPattern)
Dim matches As MatchCollection = reg_exp.Matches(sString)

Dim sString As String = """Field1a, Field1b"", Field2,
""Field3a, Field3b"", Field4("")"
Dim matches As MatchCollection = _
Regex.Matches(sString, "(""(\w+)\w*, (\2)\w*"")")
For Each m As Match In matches
Console.WriteLine(m.Value)
Next
 
E

eBob.com

Get Expresso from UltraPico (it's free!). It's an excellent tool for
experimenting with regualr expressions.

I think, THINK, that what you want to do is possible but I have never done
it.

I suspect you will need to use something called called a "backreference
construct". And a "grouping construct" - to "capture" what it is that you
need to backreference. At least that's how I'd approach it.

I'd begin by trying to match more than one of something, e.g. in "xx xx aa
yy yy yy bb" I'd try to find a pattern which would match the "xx" strings
and the "yy" strings.

Good Luck, Bob

Jerry J said:
Martin, thanks for the info, however, I realized I was not correct with my
example it should have looked like this:

I want to use the Regular Expressions MatchCollection to match multiple
patterns
in a string. I want to match the pattern that looks like "Word, Word".

Using the test code below I would want my match collection to contain
"Field1a Field1b, Field1c" and "Field3a, Field3b", but I can't get it to
work. Can
someone show me the pattern I need to use? Is it possible?


Dim sString As String = """Field1a Field1b, Field1c"", Field2, ""Field3a,
Field3b"",
Field4"

Dim sPattern As String = """[\S ]*,[\S ]*"""
Dim reg_exp As New Regex(sPattern)
Dim matches As MatchCollection = reg_exp.Matches(sString)





--
Jerry J


:

Jerry said:
I want to use the Regular Expressions MatchCollection to match multiple
patterns
in a string. I want to match the pattern that looks like "Word, Word".

Using the test code below I would want my match collection to contain
"Field1a, Field1b" and "Field3a, Field3b", but I can't get it to work.
Can
someone show me the pattern I need to use?



Dim sString As String = """Field1a, Field1b"", Field2, ""Field3a,
Field3b"",
Field4"

Dim sPattern As String = """[\S ]*,[\S ]*"""
Dim reg_exp As New Regex(sPattern)
Dim matches As MatchCollection = reg_exp.Matches(sString)

Dim sString As String = """Field1a, Field1b"", Field2,
""Field3a, Field3b"", Field4("")"
Dim matches As MatchCollection = _
Regex.Matches(sString, "(""(\w+)\w*, (\2)\w*"")")
For Each m As Match In matches
Console.WriteLine(m.Value)
Next
 
T

Tom Shelton

Martin, thanks for the info, however, I realized I was not correct with my
example it should have looked like this:

I want to use the Regular Expressions MatchCollection to match multiple
patterns
in a string. I want to match the pattern that looks like "Word, Word".

Using the test code below I would want my match collection to contain
"Field1a Field1b, Field1c" and "Field3a, Field3b", but I can't get it to
work. Can
someone show me the pattern I need to use? Is it possible?


Dim sString As String = """Field1a Field1b, Field1c"", Field2, ""Field3a,
Field3b"",
Field4"

Dim sPattern As String = """[\S ]*,[\S ]*"""
Dim reg_exp As New Regex(sPattern)
Dim matches As MatchCollection = reg_exp.Matches(sString)

Dim sPattern As String = """[\S ]*?,[\S ]*?"""

Quantifiers in regex are gready by default - in other words, they match the
longest possible expression. You need to add the ? mark to the quantifier
to tell the regex engine to not be gready, and match the shortest possible
expression...

By the way, I second the recommendation of getting expresso. Wonderful tool.
 
J

Jerry J

Tom, thank you, that worked !!!! I was so close! I will also check out
expresso!

--
Jerry J


Tom Shelton said:
Martin, thanks for the info, however, I realized I was not correct with my
example it should have looked like this:

I want to use the Regular Expressions MatchCollection to match multiple
patterns
in a string. I want to match the pattern that looks like "Word, Word".

Using the test code below I would want my match collection to contain
"Field1a Field1b, Field1c" and "Field3a, Field3b", but I can't get it to
work. Can
someone show me the pattern I need to use? Is it possible?


Dim sString As String = """Field1a Field1b, Field1c"", Field2, ""Field3a,
Field3b"",
Field4"

Dim sPattern As String = """[\S ]*,[\S ]*"""
Dim reg_exp As New Regex(sPattern)
Dim matches As MatchCollection = reg_exp.Matches(sString)

Dim sPattern As String = """[\S ]*?,[\S ]*?"""

Quantifiers in regex are gready by default - in other words, they match the
longest possible expression. You need to add the ? mark to the quantifier
to tell the regex engine to not be gready, and match the shortest possible
expression...

By the way, I second the recommendation of getting expresso. Wonderful tool.
 

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

Top