简体   繁体   中英

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
"sometextsometextsitId-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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM