Regular Expressions Question

J

Jerry J

Thisi is in VB.net, sorry...

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)
 
G

Gregory A. Beamer

Thisi is in VB.net, sorry...

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)

Your regex pattern states

I am looking for a quote, then any number of non-space characters,
followed by a comma, then any number of non-space characters followed by
a quote. I may not have this 100% correct, but that is the basics of
reading it quickly.


The problem here is you have a space after the comma in each of the
instances. Based on the input you have, I assume you can do it like
this:


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

I would personally ask this question on a regex forum, as regex for php,
java and .net are all very similar (same basic rules set), as they are
standardized. I may have given you a short direction to head, however.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 
H

Harlan Messinger

Jerry said:
Thisi is in VB.net, sorry...

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 ]*"""

This matches anything that starts with a double quote followed by zero
or more characters that are either a non-space OR an ordinary space
followed by a comma followed by zero or more characters that are either
a non-space OR an ordinary space followed by a double quote. In other
words, it will match

","

and it will also match
" , ab def xyz "

It seems to me that you don't want to be including spaces in your
"words"; for a word to be a word, it has to have at least *one*
character, which is represented by +, not *; and, judging from what
you're trying to match, you need to allow for the space following the
comma. Further, do you want non-space characters, or do you want word
characters (which includes the underscore but excludes other punctuation)?

"""[\w]+, [\w]+"""
 
P

Peter Duniho

Thisi is in VB.net, sorry... [...]

Pedantically speaking, .NET questions don't belong here at all. By
popular convention, they're tolerated or even encouraged in some cases,
but only by virtue of being presented in the context of C#. This is the
microsoft.public.dotnet.languages.csharp newsgroup, after all.

If you cannot find any way to connect your question to C# at all, you
really should post in a more appropriate newsgroup. There's a VB.NET
programming newsgroup, as well as microsoft.public.dotnet.framework. Just
because you have a chance of finding someone able to understand your
question and willing to answer it here doesn't mean this is actually the
best choice of place to post it.

Pete
 
J

Jerry J

Ok, I'll move. Didn't find the VB.net group at first.

--
Jerry J


Peter Duniho said:
Thisi is in VB.net, sorry... [...]

Pedantically speaking, .NET questions don't belong here at all. By
popular convention, they're tolerated or even encouraged in some cases,
but only by virtue of being presented in the context of C#. This is the
microsoft.public.dotnet.languages.csharp newsgroup, after all.

If you cannot find any way to connect your question to C# at all, you
really should post in a more appropriate newsgroup. There's a VB.NET
programming newsgroup, as well as microsoft.public.dotnet.framework. Just
because you have a chance of finding someone able to understand your
question and willing to answer it here doesn't mean this is actually the
best choice of place to post it.

Pete
 
J

Jerry J

Gregory,

There is a space in my brackets, it is [\S ] not [\S].

--
Jerry J


Gregory A. Beamer said:
Thisi is in VB.net, sorry...

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)

Your regex pattern states

I am looking for a quote, then any number of non-space characters,
followed by a comma, then any number of non-space characters followed by
a quote. I may not have this 100% correct, but that is the basics of
reading it quickly.


The problem here is you have a space after the comma in each of the
instances. Based on the input you have, I assume you can do it like
this:


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

I would personally ask this question on a regex forum, as regex for php,
java and .net are all very similar (same basic rules set), as they are
standardized. I may have given you a short direction to head, however.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 
J

Jerry J

Gregory, you were right, it worked. Still don't understand why my pattern
didn't.

--
Jerry J


Gregory A. Beamer said:
Thisi is in VB.net, sorry...

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)

Your regex pattern states

I am looking for a quote, then any number of non-space characters,
followed by a comma, then any number of non-space characters followed by
a quote. I may not have this 100% correct, but that is the basics of
reading it quickly.


The problem here is you have a space after the comma in each of the
instances. Based on the input you have, I assume you can do it like
this:


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

I would personally ask this question on a regex forum, as regex for php,
java and .net are all very similar (same basic rules set), as they are
standardized. I may have given you a short direction to head, however.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 
G

Gregory A. Beamer

Gregory, you were right, it worked. Still don't understand why my
pattern didn't.

Drill through your pattern again and pay attention to where you put the
spaces and the number of spaces in your pattern. The brackets [] are very
important in the answer.

Regex is a hard skill to master. I would not call myself a master these
days, even though I use it regularly. There are some good tutorials on the
web, when you have time. And don't be afraid to look at tutorials in other
languages. Regex varies a bit, but the standard rules are set down and
followed by everybody.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 

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