Regular Expression question

  • Thread starter Thread starter Jerry J
  • Start date Start date
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)
 
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
 
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
 
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
 
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.
 
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.
 
Back
Top