3

I am trying to find whether a pattern exists in a string.

Pattern I want to check is:

String starting with space or "#",
then with specific string "value1" followed by space or tab,
then "value2" and again space or tab,
ends with "value3".


Here a sample string to check:

String str = "#  value1   values2 value3";

I tried the following regular expression, but it did't work:

str.matches("^\\s+#\\s+value1\\s+value2\\s+value3");

The above pattern always returns me false. Please help with regular expression. Any help will be really appreciated.

4
  • Do you want to match a single space at the start or double space? Commented Oct 25, 2012 at 16:12
  • want to match space or tab - \\s+ Commented Oct 25, 2012 at 16:14
  • You said you want to match whether the string starts with a # or a space. But the string you posted starts with both: - # value1 values2 value3. It has both a # followed by a space. Commented Oct 25, 2012 at 16:17
  • Did you realize that String str = "# value1 values2 value3" got an S in values2 and that's because your regex is not working? (apart of \\s#, use @RohitJain one) Commented Oct 25, 2012 at 17:04

3 Answers 3

4

Try this: -

str.matches("^[ #]value1\\s+value2\\s+value3$");

[ #] - matches a space or #

\\s+ - matches 1 or more spaces. So, it would match a space or tab

NOTE: - You have values2 instead of value2 in your string.

Also, your example string you posted, has both a space and a # at the starting. But you said the string starts with a space or a #. So, it will match your string, if you remove that space after the first #, or your # before your space, which is according to what you wrote.

If you want to match a space and a # at the starting of your string, as in your current String you posted. You need to use this regex: -

str.matches("^[ #]\\svalue1\\s+value2\\s+value3$");
Sign up to request clarification or add additional context in comments.

9 Comments

u think the asker needs to match " value1 values2 value3", because your regex will not match that.
@JanDvorak Ah sorry. I got confused. Actually OP wrote something else, and his example string says something else.
@RohitJain Did you know that you can click on you score to actually see whether or not any downvotes have been made on your answer. I would assume not since your answer has recieved none, yet you seem to assume that i downvoted
@JanDvorak. OP has this string: - # value1 values2 value3 and said he want to start with a space or #, so I thought may be he wants a space and # or 2 spaces.
Perhaps he means # or space or both
|
1

Well, I can tell you right now that that regex WILL fail for that input because it requires whitespace before the #

try

str.matches("^#?\\s+value1\\s+value2\\s+value3$");

that regex should match "# value1 value2 value3" or " value1 value2 value3" with variable amounts of whitespace

8 Comments

requirement: 'starting with space or "#"'
@JanDvorak it does start with a space or #
@JanDvorak does that \\s+ after the #? not require a space in order to be matched?
\\s+ always requires a space in order to match.
@JanDvorakn then the regex I posted will only match strings that start with a # or a space won't it.
|
0

Try this regex (\s+|\s?#\s?).*value1.*(\s+|\t).*value2.*(\s+|\t).*value3.* It works even if you have whitespaces anywhere.

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.