[英]regex patern to find the words from a string
I have a string as follow. 我有一个字符串如下。
#a girlfriend, or win an argument, #女朋友,或赢得争执,
but 但
same 相同
# techniques #技术
It should give the output as follow. 它应该给出如下输出。
a girlfriend, or win an argument, 女朋友,或赢得争执,
techniques 技术
I used the pattern, "#\\s*([^#]+$|\\w*)" to do this. 我使用模式“#\\ s *([^#] + $ | \\ w *)”来执行此操作。 But it only gives output as 但是它只将输出作为
a 一种
techniques 技术
Please help me. 请帮我。
Below is the code I am using. 下面是我正在使用的代码。
Pattern pattern = Pattern.compile("#\\s*([^#\n\r]+$|\\w*)");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
} }
Try with: 尝试:
/#\W*(.*)/
It will ignore all whitespaces after #
and grab everything after. 它将忽略#
之后的所有空格,然后获取所有内容。
You could try (#[^#\\n]*\\n?)
您可以尝试(#[^#\\n]*\\n?)
Let me know what happens :) 让我知道发生什么事 :)
Use this pattern '#\\s*([^#\\n\\r]+$|\\w*)' and iterate for each match. 使用此模式'#\\ s *([^#\\ n \\ r] + $ | \\ w *)'并为每个匹配进行迭代。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] argv) throws Exception { Pattern pattern = Pattern.compile("pattern"); Matcher matcher = pattern.matcher("mystring"); // Find all matches while (matcher.find()) { // Get the matching string String match = matcher.group(); } } }
This is a really helpful site for testing your regex and see what you are going to end up with: http://myregexp.com/ 这是一个非常有用的网站,用于测试您的正则表达式并查看最终结果: http : //myregexp.com/
I think this is what you are looking for #\\s*([^#] |\\w ). 我认为这是您在寻找#\\ s *([^#] | \\ w )。 That $ is what was getting you. 那美元是什么让你。
This translates to give me everything after the first #[space] that is not a #. 这意味着我可以得到第一个非#号之后的所有信息。 You are not trying to match the second line that starts with a #. 您没有在尝试匹配以#开头的第二行。 You are taking everything after the first # that is not # 您将在第一个#之后的所有内容之后#
Hope that helps 希望能有所帮助
Here is the full code that should work for you: 以下是适合您的完整代码:
String s = "# hello!";
Pattern pattern = Pattern.compile("#\\W*([^#]*)");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
It's necessary to use matcher.group(1)
so that you get just the parenthesized capture group and not the entire expression. 必须使用matcher.group(1)
以便仅获取带括号的捕获组,而不是整个表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.