Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Help - Almost got it... 1

Status
Not open for further replies.

Quintios

Technical User
Mar 7, 2002
482
US
OK, here's the setup. I have a string from which I want to extract two digits or two letters and may include the dollar sign, $. They are always surrounded by a colon and a comma. The following works for consistently pulling out the information (incomplete code, but you get the idea of what I'm using as a basis):
Code:
sPattern = "(:)([A-Z0-9]+)(\$?)(,)"
sString = "KEY(),600259:CO,1100083:15,1101412:15$,"

That setup pulls out the following:
:CO,
:15,
:15$,

I could use the MID function to pull off the leading colon and trailing comma, but that's no fun. What I'm wondering is, how would I set up sPattern to get this result:

CO
15
15$

That's the trick. I don't want the match to return the colon or comma. I'm going through the exercise of learning regular expressions and this has provided an interesting challenge for me that I'm unable to solve.

I could also somehow use the replace method I suppose, but that has proved even more difficult for me. What I've listed above has gotten me closest to the solution, but you are free to use whatever method you want, of course. :)

Onwards,

Q-
 
all these Reg Exp questions are great, i've finally figured out the correct (VBscript) way to do this :)

First, here's the code (I used ASP):
Code:
  	<%
    Dim RegX, Match, matchesColl, sPattern, sReplaceWith, sString
    
    sPattern = &quot;:([A-Za-z0-9]{2}\$?),&quot;
    sString = &quot;KEY(),600259:CO,1100083:15,1101412:15$,&quot;

    Set RegX = NEW RegExp
    RegX.Pattern = sPattern
    RegX.Global = True
    RegX.IgnoreCase = True
    
    Set matchesColl = RegX.Execute(sString)
    
    Response.Write(&quot;Matches found: &quot;)
    For Each Match in matchesColl
      Response.Write(&quot;[<b>&quot; & Match.subMatches(0) & &quot;</b>] &quot;)
    Next
	%>
Which outputs: Matches found: [CO] [15] [15$]

That's what you want to get, right?
Here's a very good page about VBscript reg exps ( I was using a very unclear page previously which led to many problems trying to achieve the above..

Anyway.. how does it work?
[tt]sPattern = &quot;:([A-Za-z0-9]{2}\$?),&quot;[/tt]

Lets break it down:
: - matches a :
( ) - matches whatever is inside the ( ) and REMEMBERS it for later reference, only bracket things you want to use later!
[A-Za-z0-9] - matches any letter or number
{2} - matches exactly 2 of the previous [] range
\$? - matches 0 or 1 occurrence of the $ character
, - matches a ,

by only () bracketing the pattern we want to use later, we can reference it with Match.subMatches(0) - if we had several () in the pattern they would be called with subMatches(1) and so on.

Let me know if that has made everything crystal clear for you, as it has me (at last)


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
SUBMATCHES!! That's what I was missing!

You included WAY more information than I needed, but that just means you are EXTRA SUPER HELPFUL. :)

Thanks!!

Onwards,

Q-
 
yeah I think i'll write a VBscript Regular Expression FAQ for this forum- I think i've finally cracked VBscript's reg exps :)

well .. eventually i'll do it :)


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top