tags:

views:

42

answers:

2

Hi!

I have the following string "sometextsometextSiteId-111-aaaaasometext"

If the string contains "SiteId-111-aaaaa" I would like to get the 111-aaaaa part. (any number, any char)

"sometextsometextSiteId-111-aaaaasometext"  -> 111-aaaaa
"sometextsometextSiteId-123-abcdesometext"  -> 123-abcde
"sometextsometextsitId-111-aaaaasometext" -> (nothing)
"SiteId-999-QWERTPOIPOI" -> "999-QWERR"

I guess this should be possible to do?

Any hints?

Thanks Larsi

+2  A: 
(?<=SiteId-)([0-9]+-[a-zA-Z]{5})

should capture that part.

PowerShell test:

$re = '(?<=SiteId-)([0-9]+-[a-zA-Z]{5})'

'sometextsometextSiteId-111-aaaaasometext',
"sometextsometextSiteId-123-abcdesometext",
"sometextsometextsitId-111-aaaaasometext",
"SiteId-999-QWERTPOIPOI" |
% {
    $x = [regex]::Matches($_, $re)
    Write-Host $_ - $x
}

yields

sometextsometextSiteId-111-aaaaasometext - 111-aaaaa
sometextsometextSiteId-123-abcdesometext - 123-abcde
sometextsometextsitId-111-aaaaasometext - 
SiteId-999-QWERTPOIPOI - 999-QWERT
Joey
Side note: "any number, any char" is a requirement that would imply any Unicode char for me. Also you weren't quite explicit about the length of the fields. Adapt accordingly.
Joey
Well, yes a bit unclear in specyfing, but that just minor. Thanks a lot for your great and quick help. Nice also to see the powershell test. :-)
Larsi
Larsi: For this kind of things I often use PowerShell to quickly test them out and give a demonstration how it works – or whether at all. Also, since PowerShell is built on .NET the results are usually consistent with what you'd get in C#.
Joey
A: 

SiteId-(\d{3}-\D+) this should capture that.

Also you can use rubular to try your regular expressions and it has a quick regexp reference at the bottom.

Skawouter
No, it would not. It would grab "111-aaaaasometext" instead of "111-aaaaa"
Joey
You're right, I misinterpreted the question. The "any number, any char" got me confused there :)
Skawouter
Well, the five-character rule was rather unwritten, indeed. And yes, I'm also not sure whether `\D` or `\w` would be correct.
Joey