0

This is intended to be used in Java. Imagine following sample input:

WRA1007
1085808
1092650S
3901823CV

I want to match all alphabetic characters after at least one digit.

Desired output:

S
CV

Actual output:

0S
3CV

My current approach looks like this:

\d[a-zA-Z]+

The problem with this pattern is that it includes the digit beforehand too. My current solution is to remove the first character of the resulting string afterwards. And this seems quite unsatisfactory to me.

bautista
  • 765
  • 10
  • 26

2 Answers2

3

You need a lookbehind:

(?<=\d)[a-zA-Z]+

(?<=\d) means "there must be a digit before this position, but don't match it".

Demo

Alternatively, you can use a pair of () to surround the part you want to get:

\d([a-zA-Z]+)

This is called a "group", and you can get its value by calling group(1) on your Matcher.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Just a side note: `\d([a-zA-Z]+)` needs another backslash in the beginning when using with java. Otherwise it's an invalid escape sequence. Works perfectly fine though! – bautista May 29 '19 at 12:19
2

If you 'add' groups you can get group 1 that contain only letters

\d([a-zA-Z]+)
Egeo
  • 140
  • 7