I have set of inputs ++++,----,+-+-.Out of these inputs I want the string containing only + symbols.
-
`^[^+]*\+[^+]*$` – Willem Van Onsem Apr 08 '17 at 20:38
-
Thanks for your answer.But I want an example. – Rajdeep Sahoo Apr 08 '17 at 20:40
-
Please format your question and add counter examples since it's actually not clear at all. Also, show what you have already tried. – Casimir et Hippolyte Apr 08 '17 at 20:40
-
@user3761068: what do you mean with an example? – Willem Van Onsem Apr 08 '17 at 20:40
-
Possible duplicate of [Java: How do I count the number of occurrences of a char in a String?](http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string) – mickmackusa Apr 09 '17 at 04:44
4 Answers
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
.

- 154,647
- 11
- 152
- 247
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").

- 389
- 2
- 8
-
-
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
-
1I 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
For a sequence of the same symbol, use
(.)\1+
as the regular expression. For example, this will match +++
, and ---
but not +--
.

- 231,907
- 34
- 361
- 483
-
Not sure that it is what s?he's looking for, it's actually unclear. See the example `+-+-+-+-` added in the question. – Casimir et Hippolyte Apr 08 '17 at 20:45
-
-
@CasimiretHippolyte I actually find question quite unclear. `+-+-+-+-` does not *"contains only one particular symbol"*, it contains 2. – Andreas Apr 08 '17 at 20:46
-
I read the question to mean a string of arbitrary length but containing only one distinct character. – Bathsheba Apr 08 '17 at 20:50
-
@user3761068: it is more clear, but are always missing: 1) what you have already tried (SO isn't a code writing service), 2) a well formatted question. – Casimir et Hippolyte Apr 08 '17 at 20:55
-
[Answer by MauroLacy](http://stackoverflow.com/a/43300252/5221149) fixed a bug in your regex, since your regex requires 2 or more characters to match. – Andreas Apr 08 '17 at 21:10
Regex pattern: ^[^\+]*?\+[^\+]*$
This will only permit one plus sign per string.
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("[^\\+]*?\+[^\\+]*")

- 43,625
- 12
- 83
- 136