3

Description

I need a regex expression to match everything in the "value" of a key/value entry of an xml file if it finds a certain text within that line.

Problem

For the example test string:

<add key="myDatabase" value="Server=abc-lsatdm094\devbox1;Database=myDB;User Id=xxxxxx;Password=xxxxxx;" />

I need to match everything in value:

Server=abc-lsatdm094\devbox1;Database=myDB;User Id=xxxxxx;Password=xxxxxx;

IF and only if the line contains "mydb"

My Regex Expression Attempts

  • (?<=(Server=)|(Data Source=))[^;]*
  • .*(D|d)atabase=(M|m)(Y|y)(D|d)(B|b).*

Additional Test Strings

<add key="myDatabase" value="Data Source=abc-lsatdm094\devbox1;Database=myDB;User Id=xxxxxx;Password=xxxxxx;" />
<add key="myDatabase" value="User Id=xxxxxx;Password=xxxxxx;Data Source=abc-lsatdm094\devbox1;Database=mydb;" />
<add key="myDatabase-DatabaseConnectionString" value="Server=abc-lsatdm094\devbox1;Database=myDB;User Id=xxxxxx;Password=xxxxxx;" />
<add key="myDatabase-DatabaseConnectionString" value="Data Source=$LocalInstance; database=myDb; Trusted_Connection=True;" />
<add key="myDatabase-databaseConnectionString" value="database=mydb; Trusted_Connection=True;Data Source=$LocalInstance;" />
Sharpiro
  • 2,305
  • 2
  • 18
  • 27

1 Answers1

1

I actually found a solution to this as I was about to post:

(?<=value=").*(M|m)(Y|y)(D|d)(B|b).*;

Breakdown

(?<=value=")

This portion is the "look behind". So the text must contain value=" This is "matched" but not included in the capture group.

.*

This says to match all (for now)

(M|m)(Y|y)(D|d)(B|b)

This says to match mydb (uppercase and lowercase)

.*;

and continue capturing until the last instance of ;

Sharpiro
  • 2,305
  • 2
  • 18
  • 27