aravind
aravind

Reputation: 1096

Regex pattern for repeated words

I am very new to regex, I am learning it now. I have a requirement like this:

Any String starts with #newline# and also ends with #newline#. In between these two words, there could be (0 or more spaces) or (0 or more #newline#).

below is an example:

#newline#  #newline# #newline##newline# #newline##newline##newline#.

How to do regex for this?

I have tried this, but not working

^#newline#|(\s+#newline#)|#newline#|#newline#$

Upvotes: 2

Views: 154

Answers (4)

gowtham
gowtham

Reputation: 26

Please use the pattern and matches classes to identify. You can give the patternString string at runtime patternString="newline";

public void findtheMatch(String patternString)
{
     String text    ="#newline#  #newline# #newline##newline# #newline##newline##newline# ";

            Pattern pattern = Pattern.compile(patternString);
            Matcher matcher = pattern.matcher(text);

            while(matcher.find()) {
                System.out.println("found: " + matcher.group(1));
            }
}

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

Your ^#newline#|(\s+#newline#)|#newline#|#newline#$ matches either a #newline# at the start of the string (^#newline#), or 1+ whitespaces followed with #newline# ((\s+#newline#)), or #newline#, or (and this never matches as the previous catches all the cases of #newline#) a #newline# at the end of the string (#newline#$).

You may match these strings with

^#newline#(?:\s*#newline#)*$

or (if there should be at least 2 occurrences of #newline# in the string)

^#newline#(?:\s*#newline#)+$
                          ^

See the regex demo.

  • ^ - start of string
  • #newline# - literal string
  • (?:\s*#newline#)* - zero (NOTE: replacing * with + will require at least 1) or more sequences of
    • \s* - 0+ whitespaces
    • #newline# - a literal substring
  • $ - end of string.

Java demo:

String s = "#newline#  #newline# #newline##newline# #newline##newline##newline#";
System.out.println(s.matches("#newline#(?:\\s*#newline#)+"));
// => true

Note: inside matches(), the expression is already anchored, and ^ and $ can be removed.

Upvotes: 3

nosyjoe
nosyjoe

Reputation: 561

As far as I understand the requirements, it should be this:

^#newline#(\s|#newline#)*#newline#$

this will not match your example string, since it does not start with #newline#

without the ^ and the $ it matches a sub-string.

Check out http://www.regexplanet.com/ to play around with Regular Expressions.

Upvotes: 2

Muhammad Yaseen Khan
Muhammad Yaseen Khan

Reputation: 809

You can try this as well:

#newline#[\s\S]+#newline#

It says, match anything that starts with #newline# followed by any combination of whitespace or non-whitespace characters and ends with #newline#.

Upvotes: 0

Related Questions