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. 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?)
Let me know what happens :)
Use this pattern '#\s*([^#\n\r]+$|\w*)' and iterate for each match.
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/
I think this is what you are looking for #\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.
精彩评论