gishara
gishara

Reputation: 845

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());

}

Upvotes: 0

Views: 331

Answers (5)

Matthew
Matthew

Reputation: 44919

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.

Upvotes: 0

WikiPlugs
WikiPlugs

Reputation: 11

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

Upvotes: 0

Ashish
Ashish

Reputation: 5791

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();
    }
  }
}

Upvotes: 0

TimCodes.NET
TimCodes.NET

Reputation: 4689

You could try (#[^#\n]*\n?)

Let me know what happens :)

Upvotes: 0

hsz
hsz

Reputation: 152304

Try with:

/#\W*(.*)/

It will ignore all whitespaces after # and grab everything after.

Upvotes: 1

Related Questions