简体   繁体   中英

Regex Help: Trying to get numbers following a specific string

I'm trying to get the number that follows the string "WK" in a string like "TF_6502BoM_WK47.xlsx". So far I have this regex pattern /WK[0-9]+/ but I'm only getting "WK47".

So you ar looking for digits after WK and you expect any number:

(?<=WK)\\d+ will do. This means: Look for any digit numbers preceeded with WK

Sorry, your question is a little ambiguous, if you expand on your description we may be able to help more.

I've read this as you're trying to capture only the numbers? In which case you can do a named capture. The regex will parse the whole string and name the capture group containing the numbers so you can refer to it later in code.

var regex = new Regex("WK(?<numbers>\d+)");
var results = regex.Match("TF_6502BoM_WK47.xlsx");
System.WriteLine("Matched numbers: {0}", results.Groups["numbers"]);

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