Simple regex match question
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
"sometext开发者_运维知识库sometextsitId-111-aaaaasometext" -> (nothing)
"SiteId-999-QWERTPOIPOI" -> "999-QWERR"
I guess this should be possible to do?
Any hints?
Thanks Larsi
(?<=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
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.
精彩评论