-1

i had a similar question here that I was able to solve.

What I need is if a string contains word X, select only word Y.

I want to be able to say, if this string contains "azureStorage", then select, "mystorage"

<add name="azureStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=myKey" />

The closest thing I have is this regex, which works on regex101 but not in c#.

(?<=(azureStorage)...................................................AccountName=).[^;]*

Clearly this is not a good solution.

Here is a link to the example on Regex 101

Community
  • 1
  • 1
Sharpiro
  • 2,305
  • 2
  • 18
  • 27
  • 2
    Why use regex101 if you need a .NET regex? The regex101 does not support .NET syntax and features. Test at regexhero.net or regexstorm.net. – Wiktor Stribiżew Jun 13 '16 at 14:37
  • 5
    Instead of using regular expressions, why not parse the XML and then parse the connection string? – juharr Jun 13 '16 at 14:39
  • 1
    Why not use `azureStorage.*AccountName=([^;]*)` and grab captured group #1 – anubhava Jun 13 '16 at 14:40
  • Related: [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/q/1732348/216074) – poke Jun 13 '16 at 14:44

1 Answers1

1

Restrict the lookbehind to just the string azureStorage, and then grab anything after AccountName= that's not ;:

(?<=azureStorage).*AccountName=([^;]+)

You may want to add a word boundary check at the end, in case the AccountName is the last component of the connection string:

(?<=azureStorage).*AccountName=([^;]+)\b
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • This along with the @anubhava 's answer and just accessing the captured group work for me. juharr had a good answer for parsing XML, but the xml string i posted was just an example and I wanted to know how to do this for any text. Thanks. – Sharpiro Jun 13 '16 at 15:01