0

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

5
  • Is the character length till WK is fixed? Commented Dec 12, 2018 at 20:14
  • 2
    Try it with a positive lookbehind (?<=WK)[0-9]+ Demo Commented Dec 12, 2018 at 20:15
  • Nope. The only guarantee is "WK" and then a two digit number. Commented Dec 12, 2018 at 20:16
  • 1
    @Thefourthbird That seems to work perfectly thanks! Commented Dec 12, 2018 at 20:17
  • 1
    Another option you have is to use WK([0-9)]+ and access the first capture group. The postiive lookbehind should work for this purpose for sure, but capture groups are definitely something you can keep in mind for future reference. i.e. if you wanted the number that follows TF_#### AND WK##, you could capture both within the same expression. Food for thought. Commented Dec 12, 2018 at 20:25

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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"]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.