Regular Epx

  • Thread starter Thread starter guate911
  • Start date Start date
G

guate911

I am using the Regular Expression Validator, and I need to test for a
pattern that allows words in quotes, or multiple words in quotes
seperated by the word "OR".

I can get the single word in quotes check [^"]*, but I don't know how
to add the OR delimeter. Any suggestions?

See example Below.

1) "TEST"
2"TEST1" OR "Test2" OR "Test3"

Thanks
 
I think it is much more complicated than that. If i understand your question
correctly the expression i have below should work. I have tested it on a few
strings and it seems to work.

expression = ^['""]\w+['""]\s+(OR\s+['""]\w+['""])+|^['""]\w+['""]\s*$

note: expression implemented using c# regular expression metacharacters..

--Papanii
 
(?i)(?:\s*OR\s*)?"(?<word>\w+)"

This captures any sequence of word characters (digits or letters) enclosed
in quotes which are preceded by 0 or more sequences of 0 or more spaces
followed by the word "OR" followed by 0 or more spaces, and names the group
with the word in it "word." The initial sequence is a modifier indicating
that the regular expression is case-insensitive.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
(?i)(?:\s*OR\s*)?"(?<word>\w+)"

This captures any sequence of word characters (digits or letters) enclosed
in quotes which are preceded by 0 or more sequences of 0 or more spaces
followed by the word "OR" followed by 0 or more spaces, and names the group
with the word in it "word." The initial sequence is a modifier indicating
that the regular expression is case-insensitive.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net




I am using the Regular Expression Validator, and I need to test for a
pattern that allows words in quotes, or multiple words in quotes
seperated by the word "OR".
I can get the single word in quotes check [^"]*, but I don't know how
to add the OR delimeter. Any suggestions?
See example Below.
1) "TEST"
2"TEST1" OR "Test2" OR "Test3"
Thanks- Hide quoted text -

- Show quoted text -

Thank You So Much! Worked Fine

I have a question though. I am new to regular expressions, and I am
confused as to what the "(?i" Stands for?

Also,

It would not compile while I kept the grouping <word> in the
expression. I removed it, and it worked. How could I modify it, so it
would also pass if I wanted to pass test data like below:

Test Data : "test1" OR "My Test Data2"
It is failing when I include a space in between the quoted words.

Below is my code so far.

Regex regex = new Regex(@" (?i?\s)(?:\s*OR\s*)?""");
passed = regex.IsMatch(TestData);
 
I have a question though. I am new to regular expressions, and I am
confused as to what the "(?i" Stands for?

I prefer not to use Regex Options in my programming code. As I re-use
Regular Expressions, I don't want to have to remember what options to turn
on. Since the Regular Expressions syntax contains modifiers that supply
these options, I use them in the expressions.
It would not compile while I kept the grouping <word> in the
expression. I removed it, and it worked. How could I modify it, so it

(?i)(?:\s*OR\s*)?"([^"]*)"

On closer examination, I determined that my original was not robust enough.
The above should work well for you. Here's the explanation:

(?:\s*OR\s*)?

The word "OR" may precede the rest of the match, but is optional (may appear
0 or 1 times), and may have 0 or more spaces before and after it. It is in a
non-capturing group with the quantifier '?' indicating 0 or 1 times.

"([^"]*)

This is a capturing group (I removed the label, which would make it a named
capturing group). It indicates a quote character followed by 0 or more
non-quote characters, followed by a quote character. This way, there may be
any characters between the quotes.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
I have a question though. I am new to regular expressions, and I am
confused as to what the "(?i" Stands for?

I prefer not to use Regex Options in my programming code. As I re-use
Regular Expressions, I don't want to have to remember what options to turn
on. Since the Regular Expressions syntax contains modifiers that supply
these options, I use them in the expressions.
It would not compile while I kept the grouping <word> in the
expression. I removed it, and it worked. How could I modify it, so it

(?i)(?:\s*OR\s*)?"([^"]*)"

On closer examination, I determined that my original was not robust enough.
The above should work well for you. Here's the explanation:

(?:\s*OR\s*)?

The word "OR" may precede the rest of the match, but is optional (may appear
0 or 1 times), and may have 0 or more spaces before and after it. It is in a
non-capturing group with the quantifier '?' indicating 0 or 1 times.

"([^"]*)

This is a capturing group (I removed the label, which would make it a named
capturing group). It indicates a quote character followed by 0 or more
non-quote characters, followed by a quote character. This way, there may be
any characters between the quotes.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:http://www.miradyne.net




I have a question though. I am new to regular expressions, and I am
confused as to what the "(?i" Stands for?

It would not compile while I kept the grouping <word> in the
expression. I removed it, and it worked. How could I modify it, so it
would also pass if I wanted to pass test data like below:
Test Data : "test1" OR "My Test Data2"
It is failing when I include a space in between the quoted words.
Below is my code so far.
Regex regex = new Regex(@" (?i?\s)(?:\s*OR\s*)?""");
passed = regex.IsMatch(TestData);- Hide quoted text -

- Show quoted text -

Thanks for the help
 
Back
Top