Extract letter from string characters and numbers

I have these Strings:

"Turtle123456_fly.me"
"birdy_12345678_prd.tr"

I need the first words of each, i.e.

Turtle
birdy

I tried this:

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

but of course this is wrong. I am not familiar with use Pattern.

+4
source share
3 answers

Replace the material you do not want with anything:

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

to leave only the part you want.

A regular expression [^a-zA-Z]means "not writing," everything from (and including) the first nebukta to the end is "deleted."

Watch a live demo .

+3
source
String s1 ="Turtle123456_fly.me";
String s2 ="birdy_12345678_prd.tr";

Pattern p = Pattern.compile("^([A-Za-z]+)[^A-Za-z]");
Matcher matcher = p.matcher(s1);

if (matcher.find()) {
    System.out.println(matcher.group(1));
}

: ^([A-Za-z]+) - , , ( ^). [^A-Za-z] - . , , - 1 ( 1 - , ).

+3

perhaps you should try this \d+\w+.*

0
source

Source: https://habr.com/ru/post/1671906/


All Articles