.NET Framework Bookmark and Share   
 index > Regular Expressions > Regular Expression Help Needed Pls
 

Regular Expression Help Needed Pls

Hi,

My string looks like this:

myString = "|244_3_Red|244_54_Blue|555_4_Green|"

The first number beside the pipe character is passed in by the user (i.e. 244 or 555). I then need to process the two values between the pipes (i.e. If users passes 244 then process 3 and Red and then 54 and Blue. If user passes in 555 then process 4 and Green.) So far my code looks like this:

Set

objRegExp = New RegExp
objRegExp.IgnoreCase =
True
objRegExp.Global = True
objRegExp.Pattern = "\|" &userValue & "_(\d{1,2})_(.*?)\|"
Set matches = objRegExp.Execute(myString)

'Display all of the matches
For Each match in matches
quantity = match.SubMatches(0)
color = match.SubMatches(1)
'If more than one set then store into an array
Next

The problem with this pattern is that the second pipe character (|) get used by the first iteration so anything after does not get matched. In this example if the user passes in 244 then |244_3_Red| only gets matched. |244_54_Blue| does not match because the first pipe was used by the first match so the computer see "244_54_Blue|"and not "|244_54_Blue|".

I can change this to

objRegExp.Pattern =userValue & "_(\d{1,2})_(.*?)\|"

but then values like |5244_45_Blue| get matched.

Any ideas are greatly appreciated.

Thanks!
Skyfish88
You just need to modify the last part (.*?)\|
if you allow everthing in the second value you can write

objRegExp.Pattern =
"\|" &userValue &
"_(\d{1,2})_([^|]+)"

if there're only words in that value you can write
objRegExp.Pattern = "\|" &userValue & "_(\d{1,2})_(\w+)"

if you allow nothing in the second value, you should change + to *

www.wonderstudio.cn
  • Marked As Answer bySkyfish88 17 hours 7 minutes ago
  •  
Eping Wang
But in this case, I think a basic VB function can do very well.
Just use split() twice
See the code

Dim a() as string
Dim b() as string
Dim all as string
Dim i as long

all = "|244_3_Red|244_54_Blue|555_4_Green|"
a() = split(all,"|")

Fori =1 to Ubound(a)-1
b() = split(a(i),"_")
debug.print b(1)
debug.print b(2)
next
www.wonderstudio.cn
  • Marked As Answer bySkyfish88 17 hours 7 minutes ago
  •  
Eping Wang
You just need to modify the last part (.*?)\|
if you allow everthing in the second value you can write

objRegExp.Pattern =
"\|" &userValue &
"_(\d{1,2})_([^|]+)"

if there're only words in that value you can write
objRegExp.Pattern = "\|" &userValue & "_(\d{1,2})_(\w+)"

if you allow nothing in the second value, you should change + to *

www.wonderstudio.cn
  • Marked As Answer bySkyfish88 17 hours 7 minutes ago
  •  
Eping Wang
But in this case, I think a basic VB function can do very well.
Just use split() twice
See the code

Dim a() as string
Dim b() as string
Dim all as string
Dim i as long

all = "|244_3_Red|244_54_Blue|555_4_Green|"
a() = split(all,"|")

Fori =1 to Ubound(a)-1
b() = split(a(i),"_")
debug.print b(1)
debug.print b(2)
next
www.wonderstudio.cn
  • Marked As Answer bySkyfish88 17 hours 7 minutes ago
  •  
Eping Wang
Thanks Eping! ([^|]+) worked like a charm. I agree, however, that using a function will do me better. I think using regular expression for something simple like this might be overkill.
Skyfish88

You can use google to search for other answers

Custom Search

More Threads

• My Regex logic is flawed
• match all words not equal to some pattern
• need help building regex to parse search string
• RegEx Request : Get all the <img src=""> tag in a webpage.
• RegEx parsing of custom script language loops
• Extract tables from webbrowser
• How to match multi-lines with regex?
• Web Table Extraction
• Regular Expression to match proper noun
• How to get part of a url with regular expressions.