我怎么能从我的混乱中得到提到的字符串?

时间:2015-05-21 09:17:49

标签: android regex

我不知道这是否是正确的问题,但我想从特殊字符中获取字符串。

示例:

Hi this is #myFirst post on This #StackExchange.

我想输出#myFirst#StackExchange

我试过

(.*#[a-zA-Z_0-9])+\\w+

然而,它给了我整个字符串。

2 个答案:

答案 0 :(得分:1)

你可以为此使用String tokenizer。如果你的字符串每次都有#。

StringTokenizer token= new StringTokenizer(YourString, "#");

   while (token.hasMoreTokens()) {
      String value = token.nextToken(); 
      System.out.println("value from token" + value);
   }

我希望这会对你有所帮助。

答案 1 :(得分:0)

您正在捕获整个字符串,因为您在开头有.*模式。

只需使用

#[a-zA-Z0-9_]+

This is a demo显示此正则表达式匹配的内容。

此外,请注意,此正则表达式只会让您匹配基于英文脚本的主题标签。您可以使用#\w+(在Java字符串中,#\\w+)来匹配Unicode字符串,就像在Android中\w shorthand class also matches Unicode letters一样。

  

请注意,这些内置课程并不仅仅涵盖传统课程   ASCII范围。例如,\w等同于字符类   [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}]

一些sample code

import java.util.regex.*;
...
String str = "Hi this is #myFirst post on This #StackExchange.";
String rx = "#[a-zA-Z0-9_]+";
Pattern ptrn = Pattern.compile(rx);
Matcher m = ptrn.matcher(str);
while (m.find()) {
    System.out.println(m.group(0));
}

输出:

#myFirst
#StackExchange