Search code examples
javaregexsplittokenize

Extract Alpha and Numerics with decimals separately containg String using java regular expressions


I want to extract and split string which contains alpha and numeric characters separately. Eg String: 2Xccc7.08DD Output should be : [2,Xccc,7.08,DD]

I tried it using this code but it does not work.

myString.split("(\\d+|[a-zA-Z]+)");

Solution

  • For String#split(regex) you can use this regex:

    (?<=[a-z])(?=[A-Z0-9])|(?<=[0-9])(?=[A-Za-z])
    

    Working Demo