Groups | Blog | Home
all groups > vb.net > january 2005 >

vb.net : Regular expressions


Jay B. Harlow [MVP - Outlook]
1/26/2005 11:15:45 PM
Dave,
That might be the "best way", at least its the "easist way" as | the
alternation operator states "Matches any one of the terms... The leftmost
successful match wins"

Seeing as its going to find the first one (left most) it will do what you
need...

Hope this helps
Jay

[quoted text, click to view]

Dave
1/26/2005 11:24:01 PM
I was wondering if someone could help me with a regular expression problem.
I have a string of characters and numbers that will contain some information
about chemical formulas. I need to parse out of the string what elements it
contains and which numbers are associated with those elements. The problem
I'm having is that some elements contain 2 characters and some just one. For
example, the elements Br and B. I had originally set up a search pattern for
the Regex object to have a complete list of the periodic table "[B|Br|
etc.]" and was going to use matches method to get a collection of all
elements matched in the string.

dim myRegex as regularexpressions.regex("[B|Br| the rest of periodic table
here ]", regexoptions.ignorecase)
dim matchesMade as regularexpressions.matchescollection
matchesMade = myregex.Matches(aString)
for each matchMade as regularexpressions.match in matchesMade
' do stuff to the match here
next

The problem is the logic above would get a match on B even if the formula
really contain Br and I need to distinguish between the two.

Any suggestions on how I do this would be appreciated.

Thanks,
Dave

Dave
1/26/2005 11:31:38 PM
I think a way around this is to change the order of my pattern expressions
so that the multiple character elements shown up first in the list ( ie
["Br|Bk|Bi|Be|Ba|B"]). Not sure if this is the best way around the problem
but it seems to work.

Dave

[quoted text, click to view]

Dave
1/27/2005 8:14:46 PM
Thanks Jay & yes that does help.

Dave

[quoted text, click to view]

AddThis Social Bookmark Button