javaregex

Java Regex to extract numbers and strings from a string


I want to extract numbers and strings from a string.

Ex.

"TU111 1998 SUMMER”

"TU-232 SU 1999"

"TU232 1999 SUMMER"

I was able to get it using two patterns Pattern.compile("\\d+") and Pattern.compile("[a-zA-Z]+") Is there a way to get it using one pattern?

The expected outcome should be

1=>TU 2=> 111/232 3=>1998/1999 4=>SUMMER/SU


Solution

  • You can just pipe the two regexes together:

    [0-9]+|[a-zA-Z]+

    Demo