如何获取以特定字符开头的字符串的字母,避免其他特殊字符
How to get the letters of a string which starts with a certain character avoiding other special characters
我有以下字符串。
#firstName.concat(' ').concat(#lastName)
我需要获取以“#”开头的名称。但它后面不应跟其他特殊字符。所以在这种情况下,我需要 "firstName" 和 "lastNAme" 作为输出。我试过下面的方法。但它抛出模式不匹配异常。任何帮助将不胜感激。
private static void getTokens(String value) {
Pattern p = Pattern.compile("\(^#\)");
Matcher m = p.matcher(value);
String s = m.group(1);
System.out.println("answer : " + s);
}
你需要一个像
这样的简单正则表达式
#(\w+)
匹配#
,然后用\w+
捕获1+个后续单词字符,然后你需要运行匹配器.find()
里面while
循环。
Pattern p = Pattern.compile("#(\w+)");
Matcher m = p.matcher(value);
while (m.find()) {
String s = m.group(1);
System.out.println("answer : " + s);
}
我有以下字符串。
#firstName.concat(' ').concat(#lastName)
我需要获取以“#”开头的名称。但它后面不应跟其他特殊字符。所以在这种情况下,我需要 "firstName" 和 "lastNAme" 作为输出。我试过下面的方法。但它抛出模式不匹配异常。任何帮助将不胜感激。
private static void getTokens(String value) {
Pattern p = Pattern.compile("\(^#\)");
Matcher m = p.matcher(value);
String s = m.group(1);
System.out.println("answer : " + s);
}
你需要一个像
这样的简单正则表达式#(\w+)
匹配#
,然后用\w+
捕获1+个后续单词字符,然后你需要运行匹配器.find()
里面while
循环。
Pattern p = Pattern.compile("#(\w+)");
Matcher m = p.matcher(value);
while (m.find()) {
String s = m.group(1);
System.out.println("answer : " + s);
}