Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help to split string in java using regex

Tags:

java

regex

I have a string like "portal100common2055".

I would like to split this into two parts, where the second part should only contain numbers.

"portal200511sbet104" would become "portal200511sbet", "104"

Can you please help me to achieve this?

like image 605
Raja Avatar asked Feb 25 '23 02:02

Raja


1 Answers

Like this:

    Matcher m = Pattern.compile("^(.*?)(\\d+)$").matcher(args[0]);
    if( m.find() ) {
        String prefix = m.group(1);
        String digits = m.group(2);
        System.out.println("Prefix is \""+prefix+"\"");
        System.out.println("Trailing digits are \""+digits+"\"");
    } else {
        System.out.println("Does not match");
    }
like image 200
Simon G. Avatar answered Mar 05 '23 20:03

Simon G.