简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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