1

I am studying the zero-width RegEx. First, I test my pattern and text at http://testregex.com/ and it works well. Then I test them in my Java program, but they don't match. So I'd like to make it clear the cause of problem. Any reply would be appreciated. Thanks!

pattern:`\w*(?=ing)`

text:I’m singing while you’re dancing

Java code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegexDemo {

  public static void main(String[] args) {
    RegexDemo demo = new RegexDemo();
    System.out.printf("%b%n", demo.zeroWidthAssertionEarly());
  }

  public boolean zeroWidthAssertionEarly()  
  {  
    String reg="\\w*(?=ing)";
    String word = "I’m singing while you’re dancing";
    boolean tem=false;  

    Pattern pattern = Pattern.compile(reg);  
    Matcher matcher = pattern.matcher(word);  

    tem = matcher.matches(); 

    return tem;  
  } 
}

Thank you in advance. Now I hope I do better understand the difference between match() and find(), and I modified my code as follows:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo {
  public static void main(String[] args) {
    RegexDemo demo = new RegexDemo();
    System.out.printf("%b%n", demo.zeroWidthAssertionEarly());
  }

  public boolean zeroWidthAssertionEarly()  
  {  
    //匹配以ing结尾的单词
    String reg="\\w*(?=ing)";
    String word = "I’m singing while you’re dancing";//
    boolean tem=false;  

    Pattern pattern = Pattern.compile(reg);  
    Matcher matcher = pattern.matcher(word);  

    while(matcher.find()){
      System.out.printf("start = %d%n", matcher.start());
      System.out.printf("end = %d%n", matcher.end());
    }        
    return tem;  
  } 
}

Its output confused me:

start = 4
end = 8
start = 8
end = 8
start = 25
end = 29
start = 29
end = 29
false

So my new question is: Why the output is not as follows?

start = 4
end = 8
start = 25
end = 29
false
2
  • 2
    matches() requires a full string match. Try find(). BTW, you have not indicated what you need to get as a result. Just true or false? Commented Apr 17, 2016 at 15:20
  • Your help is very useful,thanks a lot! I have read some related answers and have some understand of find().But I encounter a new problem if find() and start()/end().Can you give me some prompt again? Commented Apr 18, 2016 at 13:06

1 Answer 1

0

The answer for your updated question is: use

String reg="(?U)\\w+(?=ing\\b)";

See the Java demo

Explanation of the pattern:

  • \w+ - match 1 or more alphanumeric or inderscore characters that are
  • (?=ing\b) - followed by ing + a trailing word boundary.

The (?U) is necessary to make sure the word boundary works alright with Unicode strings.

Your pattern does not check if ing is located at the end of the word, and it allows matching ing at the beginning of a word which is not what you are after (I guess, matching -ing forms).

Sign up to request clarification or add additional context in comments.

1 Comment

I see the reason.In my code, the "\w*" match 0 or more characters, so the "ing" matches itself.Get the usage of "?U",exciting!Thank you again :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.