Reputation: 1463
How do I check the whether '_' is in a string or not using regular expressions?
Ex: _a,a_ab_a_b.
Upvotes: 1
Views: 118
Reputation: 1024
As Kobi mentions, String.contains() is much more efficient.
However, if need to check if a pattern occurs anywhere in a String, The following sample shows one how to do it:
Pattern p = Pattern.compile(myPattern);
Matcher m = p.matcher(stringToCheck);
if (m.find()) {
System.out.println("String contains pattern");
}
Upvotes: 0
Reputation: 27575
You use the pattern
'^(?=.*_.*)$'
to be sure '_' is in the string
or
'^(?!.*_.*)$'
or '^(?=[^_]*)$'
to be sure it isn't there
But it has no sense to use this alone. It must be a part of a larger pattern.
.
I must do a more explanatory answer and CORRECT THE ABOVE PATTERNS.
.
I wrote the above answer rapidly, without testing the proposed patterns. My idea was, and is, that there is no justification to use them "nude" as they are, because, as Kobi "underscored" it :) , it is possible to test the presence of '_' without a regex, and so it's preferable to do as he proposed when it is the only aim.
But I wrote my answer, first to give the hint to the questioner eager to know, and second to give the hint that will allow to write a larger pattern.
My idea is indeed that one of these 3 elementary patterns can be associated with every other real pattern that searches some particular string(s).
.
Writing too rapidly this answer led me to make a mistake in the 3 patterns.
I had first written '^(?=.*_.*$)'
, for exemple. But I modified it before posting, with the idea that the rest of a larger pattern should be inserted between '^(?=.*_.*)'
and '$'
. That's right, a complement must be inserted before the ending character '$'
that means "the end of the string is here". But the fact is that each of the 3 correct patterns MUST also keep the '$'
character untouched INSIDE the parenthesis.
.
The "nude" incorrect patterns are:
'^(?=.*_.*)$'
'^(?!.*_.*)$'
'^(?=[^_]*)$'
.
The right ones are:
'^(?=.*_.*$)'
'^(?!.*_.*$)'
'^(?=[^_]*$)'
Then you add complementary elementary pattern(s) to them according what you want to catch.
.
For exemple , if you want to catch the first number in a string in which there must not be the underscore, the pattern will be:
'^(?=[^_]*$).*?(\d+)'
and the number '123' in a string 'kfgv 123 tg 4567 jkfv' will be catched by the group 1.
But now, if you want to catch the last number ( = the more in proximity to the end of the string) in which there must not be any undesrcore, the pattern will have to be:
'^(?=[^_]*$).*?(\d+)[^\d]*$'
In this case, there are two characters '$'
in the fill pattern and the important point is that the '$'
present in '^(?=[^_]*$)'
is absolutely necessary because without this character at this place, there would be false result:
'^(?=[^_]*).*?(\d+)[^\d]*$'
matches with the string 'kfg_v 123 tg 4567 jkfv' and the group 1 matches with '4567' while it shouldn't because of the underscore in the analysed string
On the contrary, '^(?=[^_]*$).*?(\d+)[^\d]*$'
doesn't match with the same string and it is what is aimed at.
Upvotes: 1
Reputation: 138027
Using Java, you can use string.contains
:
boolean hasUnderscore = str.contains("_");
Upvotes: 7