views:

20

answers:

1

I've got a multiline string like this. Ity has line feeds.

[Site Url="http://medportal.domain.edu" Owner="DOMAIN\user1" SecondaryOwner="DOMAIN\user2" ContentDatabase="WSS_Content_$1" StorageUsedMB="0.8" StorageWarningMB="0" StorageMaxMB="0" /] [Site Url="http://medportal.domain.edu/sites/ahSC" Owner="DOMAIN\user1" ContentDatabase="WSS_Content_ahSC" StorageUsedMB="22.3" StorageWarningMB="0" StorageMaxMB="0" /] [Site Url="http://medportal.domain.edu/sites/ARCTIC" Owner="DOMAIN\user1" ContentDatabase="WSS_Content_ARCTIC" StorageUsedMB="0.1" StorageWarningMB="0" StorageMaxMB="0" /]

I need to extract and format out strings that look like this:

stsadm.exe -o deletecontentdb -url "http://medportal.domain.edu" -databasename "WSS_Content_$1" -databaseserver myfixedservername

Where the two arguments are Url and ContentDatabase.

This pattern almost works but it picks up extra stuff and can't handle multiline

(.)\s(Url=)(?.)\s(.)\s(ContentDatabase=)(?.)\s(StorageUsedMB=)(.*)

replace:

stsadm.exe -o deletecontentdb -url ${url} -databasename ${databasename} -databaseserver myfixedservername

Thank you.

A: 

Your current pattern is greedy and matches more than intended judging from the resulting replacement. "Greedy" means the usage of .* is gobbling up more than you intend, so to make it non-greedy you need to add a ? after it so it matches the least amount of characters possible: .*?.

A pattern like \".*\" is greedy because you intend for it to stop at the first quote encountered, but it actually continues matching content till it hits the final quote in the string, if one exists. The non-greedy solution is to use \".+?\" or \"[^\"]+\".

Try this pattern instead:

string pattern = @"\[.+?Url=(?<url>"".+?"").+?ContentDatabase=(?<databasename>"".+?"").+?]";
string replacement = "stsadm.exe -o deletecontentdb -url ${url} -databasename ${databasename} -databaseserver myfixedservername";
string result = Regex.Replace(input, pattern, replacement);
Console.WriteLine(result);
Ahmad Mageed