C# Regular Expressions

  • Thread starter Thread starter PJC
  • Start date Start date
P

PJC

Not sure if you can help but I'm having a little trouble with c# and
some regular expressions.

This is the example I am trying to use:

Regex r = new
Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",RegexOptions.Compiled);
return r.Match(url).Result("${proto}${port}");

For example, "http://www.contoso.com:8080/letters/readme.html" would
return "http:8080".

This works OK but I want to try to appy it to this string:
string subject = "SAP TMS Galileo Gateway Message Log (Langley
Development) (10\\15\\33)";

and extract Langley development then 10 then 15 and then 33.

Any help would be really appreciated, its just the Regex I can't get
right..

Thanks
 
PJC said:
Not sure if you can help but I'm having a little trouble with c# and
some regular expressions.

This is the example I am trying to use:

Regex r = new
Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",RegexOptions.Compiled);
return r.Match(url).Result("${proto}${port}");

For example, "http://www.contoso.com:8080/letters/readme.html" would
return "http:8080".

This works OK but I want to try to appy it to this string:
string subject = "SAP TMS Galileo Gateway Message Log (Langley
Development) (10\\15\\33)";

and extract Langley development then 10 then 15 and then 33.

Any help would be really appreciated, its just the Regex I can't get
right..

Thanks

aehm? What do you want? Do you want to know why theres no match in your
second string? or do you want a regular expression that does the job in
your second string?
 
Hi PJC,

The regular expression below works, but assumes that there are only single
backslashes in your string (I presumed that the syntax in your message
indicated that the first backslash is an escape character).

(?-i)(\(Langley[\s]*Development\))|(?<!\\)([\d]{2})|(?>([\d]{2})(?!\\))|([\d]{2})

It puts the 3 2-digit sequences into a group for each, so that you can
identify the 2-digit sequence in each match by it's group:

"10\15\33" - "10" is group 2, "15" is group 4, and "33" is group 3.

If you don't need the extra grouping, you can use an abbreviated version:

(?-i)(\(Langley[\s]*Development\))|([\d]{2})

In this version, you can identify the 2-digit sequences by their order in
the matches:

"10\15\33" - "10" is the first match in group 2, "15" is the second, and
"33" is the third.

In both cases, "(Langley Development)" is group 1. It is case-insensitive. I
don't know if you need to include the parentheses or not, but it's easy
enough to remove them from the group:

(?-i)\((Langley[\s]*Development)\)|([\d]{2})

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
To a tea you esteem
a hurting back as a wallet.
 
Back
Top