Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract letter from String characters and numbers

Tags:

java

regex

I have these Strings:

"Turtle123456_fly.me"
"birdy_12345678_prd.tr"

I want the first words of each, ie:

Turtle
birdy

I tried this:

 Pattern p = Pattern.compile("//d");
 String[] items = p.split(String);

but of course it's wrong. I am not familiar with using Pattern.

like image 471
Moudiz Avatar asked Mar 10 '23 16:03

Moudiz


1 Answers

Replace the stuff you don't want with nothing:

String firstWord = str.replaceAll("[^a-zA-Z].*", "");

to leave only the part you want.

The regex [^a-zA-Z] means "not a letter", the everything from (and including) the first non-letter to the end is "removed".

See live demo.

like image 139
Bohemian Avatar answered Mar 19 '23 02:03

Bohemian