0

I cannot seem to figure this out this out for the life of me.

I am simply trying to match this string with double quotes "emailaddress":"blah@Blah.com"

I've tried several attempts at the regex and this is the closest.

Regex test = new Regex("@\"emailAddress\":\"blah@Blah.com\"");

What is wrong with my syntax?

user1158745
  • 2,402
  • 9
  • 41
  • 60
  • 2
    Redundant `@` at the start and an unescaped dot. – Wiktor Stribiżew Jan 29 '16 at 15:43
  • 2
    And if it's case sensitive, the "a" in "address" is different. – James Thorpe Jan 29 '16 at 15:44
  • 2
    Also, if you're planning on expanding this to match any email address, [this is a good read](http://www.regular-expressions.info/email.html) on the topic. – James Thorpe Jan 29 '16 at 15:45
  • 1
    How do you escape an period? – user1158745 Jan 29 '16 at 15:46
  • 1
    Escaping in a regular string literal is done with ``\\``. In a verbatim string literal - with ``\``. – Wiktor Stribiżew Jan 29 '16 at 15:48
  • Well the thing is the email address word itself can change the part that will stay static is "emailaddress": – user1158745 Jan 29 '16 at 15:48
  • Then someone must close this question as a dupe of [*extract all email address from a text using c#*](http://stackoverflow.com/questions/2333835/extract-all-email-address-from-a-text-using-c-sharp). – Wiktor Stribiżew Jan 29 '16 at 15:49
  • 1
    Another thing, and I could well be wrong, but this looks suspiciously like a section from a JSON string? If it is, you may be better off using a JSON parser to extract the info, at least you're only left with the email address to validate in whichever fashion you prefer. – James Thorpe Jan 29 '16 at 15:51

2 Answers2

1

Use Regex.Escape() to do the escaping for use in the regular expression for you. Then you only have to escape the double quotes the usual way:

    var term = @"""emailaddress"":""blah@Blah.com""";

    Regex test = new Regex(Regex.Escape(term), RegexOptions.IgnoreCase);

Use the RegexOptions.IgnoreCase flag in the Constructor to specify that case should be ignored.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
0

Regex is case sensitive by default. You need to change the "A" in "emailAddress" to a lower case "a":

\"emailaddress\":\"blah@Blah.com\"
chief7
  • 14,263
  • 14
  • 47
  • 80