0

I'm trying to match 0 or more strings of letters, numbers, and white space between quotation marks ("), these sets can be separated by white space or not, and the string will start with a key word to identify what to do with the matched sets.

The simplest example is below:

\\ inString = test "1" "a""3";
Regex regEx = new Regex("@(\"[0-9 a-z]*\")", RegexOptions.IgnoreCase);
Match match = regEx.Match(inStr);

The match is not a success, let alone containing the 3 expected results

However via http://regexhero.net the match is a success - I'm using regexhero as its SilverLight based so is using the .NET Regex engine...

Regexhero settings:

Regular Expression

(\"[0-9 a-z]*\")

Target String

test "1" "b""3"

Result

1: "1"
1: "b"
1: "3"

Can anyone explain whats wrong with my implementation?

Morvael
  • 3,478
  • 3
  • 36
  • 53
  • 1
    Double quotes in a verbatim string literal must be declared with `""`. I guess you wanted `@"""[0-9 a-z]*"""`. See the [demo](http://regexstorm.net/tester?p=%22%5b0-9+a-z%5d*%22&i=test+%221%22+%22a%22%223%22) of the regex. I think this is just a typo. I suggest closing the question. – Wiktor Stribiżew Feb 09 '16 at 14:21
  • 4
    i just noticed that the @ is within the string. why is it there? if it is outside, you would need double quotes (as @WiktorStribiżew pointed out). it doesnt make any sense inside – x4rf41 Feb 09 '16 at 14:23
  • Accepted the answer below - yes it was a typo. – Morvael Feb 09 '16 at 14:33
  • @Morvael look my answer You have to change something more – blogprogramisty.net Feb 09 '16 at 14:41
  • @goodeinstein Yes I know - it was just sample code, the actual implementation looks little like the code in the question. It was simply that I had put the @ in the wrong place and then developed a blind spot for it. – Morvael Feb 09 '16 at 15:58

2 Answers2

3

I expect your regex is meant to be:

Regex regEx = new Regex("(\"[0-9 a-z]*\")", RegexOptions.IgnoreCase);

or (exactly the same)

Regex regEx = new Regex(@"(""[0-9 a-z]*"")", RegexOptions.IgnoreCase);

It looks like this question might be a fruitful read for you:

What's the use/meaning of the @ character in variable names in C#?

Community
  • 1
  • 1
spender
  • 117,338
  • 33
  • 229
  • 351
0

You must change the regexp patern:

"@(\"[0-9 a-z]*\")"

to

@"(""[0-9 a-z]*"")"

Also You need change

Match match = regEx.Match(inStr);

to

MatchCollection match = regEx.Matches(intstr); 

Because Matches searches an input string for all occurrences of a regular expression and returns all the matches but Match searches the specified input string for the first occurrence of the regular expression

If You want result like this:

1: "1"
1: "b"
1: "3"

You need to change to Matches

blogprogramisty.net
  • 1,714
  • 1
  • 18
  • 22