2

I wrote a regEx program in java. I think that is true but The result is different. please help me to fix that.

my code:

String text    ="My wife back me up over my decision to quit my job";

String patternString = "[/w/s]*back(\\s\\w+\\s)*up[/w/s]*.";

Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);

Matcher matcher = pattern.matcher(text);

boolean matches = matcher.matches();

System.out.println("matches = " + matches);

output: matches = false

I'm new in java programming. I want to write a program with regEx to test match of "back up" in the input sentence.

Thanks for your attention.

3
  • 1
    What is expected output and what you getting? Commented Mar 23, 2013 at 7:55
  • Try using \\w\\s instead of /w/s. Commented Mar 23, 2013 at 7:58
  • I think my regEx is true but the output is false. i want to right a program for my previous question. please help me. my previous question link: stackoverflow.com/questions/15563185/… Commented Mar 23, 2013 at 7:58

2 Answers 2

3

I think you pattern should be like this:

String patternString = "[\\w\\s]*back(\\s\\w+\\s)*up[\\w\\s]*.";
Sign up to request clarification or add additional context in comments.

1 Comment

Jho, thanks for your answer. Can you answer my question in my previous question: stackoverflow.com/questions/15563185/…
1

You are using forward slashes instead of backslashes:

String patternString = "[/w/s]*back(\\s\\w+\\s)*up[/w/s]*.";
                         ^ ^                       ^ ^

The two are not interchangeable (and don't forget that backslashes need to be doubled up).

Comments

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.