0

I have set of inputs ++++,----,+-+-.Out of these inputs I want the string containing only + symbols.

Rajdeep Sahoo
  • 77
  • 1
  • 2
  • 9

4 Answers4

2

If you want to see if a String contains nothing but + characters, write a loop to check it:

private static boolean containsOnly(String input, char ch) {
    if (input.isEmpty())
        return false;
    for (int i = 0; i < input.length(); i++)
        if (input.charAt(i) != ch)
            return false;
    return true;
}

Then call it to check:

System.out.println(containsOnly("++++", '+')); // prints: true
System.out.println(containsOnly("----", '+')); // prints: false
System.out.println(containsOnly("+-+-", '+')); // prints: false

UPDATE

If you must do it using regex (worse performance), then you can do any of these:

// escape special character '+'
input.matches("\\++")

// '+' not special in a character class
input.matches("[+]+")

// if "+" is dynamic value at runtime, use quote() to escape for you,
// then use a repeating non-capturing group around that
input.matches("(?:" + Pattern.quote("+") + ")+")

Replace final + with * in each of these, if an empty string should return true.

Andreas
  • 154,647
  • 11
  • 152
  • 247
1

The regular expression for checking if a string is composed of only one repeated symbol is

^(.)\1*$

If you only want lines composed by '+', then it's

^\++$, or ^++*$ if your regex implementation does not support +(meaning "one or more").

Mauro Lacy
  • 389
  • 2
  • 8
  • Yup, that's the one. – Bathsheba Apr 08 '17 at 20:56
  • Nope it's not. That doesn't check for a *particular* symbol, like `+`. It checks if all chars are the same. --- Also, it's a duplicate of [answer by Bathsheba](http://stackoverflow.com/a/43300131/5221149). – Andreas Apr 08 '17 at 21:02
  • @Andreas, yes, it checks if **all** the chars are the same. No, it's not a duplicate of Bathsheba's answer. – Mauro Lacy Apr 08 '17 at 21:05
  • @MauroLacy Ok, you changed `+` (1 or more) to `*` (0 or more). Other than that it's a duplicate, since `^` and `$` are unnecessary when using `matches()`. – Andreas Apr 08 '17 at 21:07
  • @Andreas. So? The answer is therefore more general, and therefore, correct. That is **the** regex to check for that, independently of calling conventions. And, addressing the first part of your comment, a string of only one char is composed of all the same chars, you know? – Mauro Lacy Apr 08 '17 at 21:10
  • Yes, and you regex will match a string of *any* one char, but OP only wants to match string consisting of nothing but `+` characters. *"I have set of inputs `++++`, `----`, `+-+-`. Out of these inputs I want the string containing only `+` symbols."* So, only the first one. You match the second one too. – Andreas Apr 08 '17 at 21:12
  • Well, it seems to me that now you are misunderstanding OP's question. – Mauro Lacy Apr 08 '17 at 21:15
  • 1
    I agree, *you* are misunderstanding the question. *"I want the string containing only `+` symbols"* is pretty clear. OP doesn't want a string containing only `-` symbols, or any other symbols. – Andreas Apr 08 '17 at 21:16
0

For a sequence of the same symbol, use

(.)\1+

as the regular expression. For example, this will match +++, and --- but not +--.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Regex pattern: ^[^\+]*?\+[^\+]*$

This will only permit one plus sign per string.

Demo Link

Explanation:

^         #From start of string
[^\+]*    #Match 0 or more non plus characters
\+        #Match 1 plus character
[^\+]*    #Match 0 or more non plus characters
$         #End of string

edit, I just read the comments under the question, I didn't actually steal the commented regex (it just happens to be intellectual convergence):

Whoops, when using matches disregard ^ and $ anchors.

input.matches("[^\\+]*?\+[^\\+]*")
mickmackusa
  • 43,625
  • 12
  • 83
  • 136