aklassen
aklassen

Reputation: 75

How to use regex to separate a string that contains any character and then ends in exclusively numbers?

So I've been trying to come with a regex that separates these kinds of strings: A100, A-100, A1-100, A1_100, A1A100, "A-100", and many other examples.

The strings exclusively "end" with only numbers, and I say "end" because they can be in quotations, and technically it's not the end of the string, it's a word boundary though.

What I need is to get both things, whatever is behind only numbers and the string containing only numbers, I need to be able to separate them because I might need to do some additions to the only numbers part.

What I've tried is:

All of this with an implementation that works with C# and .NET. Any guidance?

Upvotes: 2

Views: 193

Answers (1)

41686d6564
41686d6564

Reputation: 19661

You may use the following pattern:

\b([A-Za-z]+(?:[A-Za-z0-9]*[A-Za-z_\-])?)(\d+)\b

Demo.

Details:

  • \b - Word boundary.
  • ( - Start of group 1.
    • [A-Za-z]+ - Match one or more letters.
    • (?: - Start of a non-capturing group.
      • [A-Za-z0-9]* - Match zero or more alphanumeric characters.
      • [A-Za-z_\-] - Match a single letter, underscore, or hyphen.
    • )? Close the non-capturing group and make it optional.
  • ) - Close group 1.
  • (\d+) - Match one or more digits and capture them in group 2.
  • \b - Word boundary.

Note: It's not entirely clear from your question what characters are accepted. This assumes letters, digits, an underscore, and a hyphen. Feel free to add more characters in the appropriate character class if you need to support more.

Upvotes: 4

Related Questions