views:

119

answers:

2

How do you match more than one pattern in vbscript?

Set regEx = New RegExp

regEx.Pattern = "[?&]cat=[\w-]+" & "[?&]subcat=[\w-]+" // tried this
regEx.Pattern = "([?&]cat=[\w-]+)([?&]subcat=[\w-]+)" // and this

param = regEx.Replace(param, "")

I want to replace any parameter called cat or subcat in a string called param with nothing.

For instance

string?cat=meow&subcat=purr or string?cat=meow&dog=bark&subcat=purr

I would want to remove cat=meow and subcat=purr from each string.

+1  A: 
regEx.Pattern = "([?&])(cat|dog)=[\w-]+"

param = regEx.Replace(param, "$1") ' The $1 brings our ? or & back
Sean Bright
Thanks, but how would I string two completely different patterns together? Let's say remove anything that matches "dog" or "cat"?
George
@George: see my edit
Sean Bright
George
+1  A: 

Generally, OR in regex is a pipe:

[?&]cat=[\w-]+|[?&]subcat=[\w-]+

In this case, this will also work: making sub optional:

[?&](sub)?cat=[\w-]+

Another option is to use or on the not-shared parts:

[?&](cat|dog|bird)=[\w-]+
Kobi
Thanks. I was doing the | before, but I realized the reason it wasn't working properly was because I didn't have regEx.Global = True set.
George