简体   繁体   English

从字符串字符和数字中提取字母

[英]Extract letter from String characters and numbers

I have these Strings : 我有这些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 . 我不熟悉使用Pattern

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));
}

Explanation: The first part ^([A-Za-z]+) is a group that captures all the letters anchored to the beginning of the input (using the ^ anchor). 说明:第一部分^([A-Za-z]+)是一个捕获锚定到输入开头的所有字母的组(使用^锚)。 The second part [^A-Za-z] captures the first non-letter, and serves as a terminator for the letters sequence. 第二部分[^A-Za-z]捕获第一个非字母,并用作字母序列的终止符。 Then all we have left to do is to fetch the group with index 1 (group 1 is what we have in the first parenthesis). 然后我们剩下要做的就是获取索引为1的组(组1是我们在第一个括号中所拥有的)。

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". 正则表达式[^a-zA-Z]表示“不是字母”,从第一个非字母到结尾的所有内容都被“删除”。

See live demo . 查看现场演示

也许你应该试试这个\\d+\\w+.*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM