0

I apologize in advance if this already has an answer but the best I could find relevant was: Regex to test if a string ends with a number

Which is not exactly what I am looking for. I am trying to figure a regex for a fixed string ending with number.

So lets say my fixed string is "This is Sparta"

So the regex would match for

"This is Sparta9" "This is Sparta100" "This is Sparta87" "This is Sparta21" "This is Sparta8"

But will not match for anything else before and after the string so "This is Sparta7e" would not match "Hi, This is Sparta7" would not match "This is Sparta and 7" would not match

So basically a fixed String (CONSTANT) ending with number is the kind of regex I am looking for.

Community
  • 1
  • 1
Nick Div
  • 4,261
  • 8
  • 55
  • 108

1 Answers1

1

You just need your constant followed by at least one digit (\d+):

private static Pattern SPARTA_REGEX = Pattern.compile("This is Sparta(\\d)+");
Mureinik
  • 252,575
  • 45
  • 248
  • 283