test method of RegExp GWT/Javascript - java

I want to detect if a String is a decimal by using a regular expression. My question is more on how to use the regular expression mechanism than detecting that a String is a decimal. I use the RegExp class provided by GWT.
String regexDecimal = "\\d+(?:\\.\\d+)?";
RegExp regex = RegExp.compile(regexDecimal);
String[] decimals = { "one", "+2", "-2", ".4", "-.4", ".5", "2.5" };
for (int i = 0; i < decimals.length; i++) {
System.out.println(decimals[i] + " "
+ decimals[i].matches(regexDecimal) + " "
+ regex.test(decimals[i]) + " "
+ regex.exec(decimals[i]));
}
The output:
one false false null
+2 false true 2
-2 false true 2
.4 false true 4
-.4 false true 4
.5 false true 5
2.5 true true 2.5
I was expecting that both methods String.matches() and RegExp.test() return the same result.
So what's the difference between
both methods?
How to use the RegExp.test() to get the same behaviour?

Try to change the regex to
"^\\d+(?:\\.\\d+)?$"
explain
double escape is because we're in Java...
regex start with ^ to forces the regex to match from the very start of the string.
regex end with $ to forces the regex to match from the very end of the string.
this is how you should get String.matches() to do the same as GWT RegExp.test()

I don't know the difference, but I would say that RegExp.test() is correct, because your regex matches as soon as there is a digit within your string and String.matches() behaves like there where anchors around the regex.
\\d+(?:\\.\\d+)?
Your non capturing group is optional, so one \\d ([0-9]) is enough to match, no matter what is around.
When you add anchors to your regex, that means it has to match the string from the start to the end, then RegExp.test() will probably show the same results.
^\\d+(?:\\.\\d+)?$

Related

JAVA regex pattern if statement

This is what i have already.
^[abceghj-prstw-z][a-np-z]$
I am trying to form regex pattern with these requirements:
First position can be any letter but d,f,i,q,u,v.
Second position can be any letter but o.
The first and second position can't be BG, GB, NK, KN, TN, NT, ZZ.
So for example string "ap" = true.
ao = false (because second position is o).
gb = false (because it cant be gb)
I am pretty new with regular expressions so any help would be great.
Thanks.
You need to make use of negative lookahead to make the regex fail if those specific patterns exist:
^(?i)(?!(bg)|(gb)|(nk)|(kn)|(tn)|(nt)|(zz))[abceghj-prstw-z][a-np-z]$
(?i) makes it case-insensitive.
As answered here you may add negative lookahead to exclude forbidden symbols from the beginnig of your regex:
^(?!bg|gb|nk|kn|tn|nt|zz)[abceghj-prstw-z][a-np-z]$
You can use negative lookaheads or negative lookbehinds if you don't want to check the exceptions (gb, ...) manually. Here's an example with a negative lookbehind:
Pattern p = Pattern.compile("[abceghj-prstw-z][a-np-z](?<!gb|bg|nk|kn|tn|nt|zz)", Pattern.CASE_INSENSITIVE);
List<String> inputs = Arrays.asList("ap", "apo", "AP", "GB", "gb", "gg");
for (String input : inputs) {
System.out.println(input + " " + p.matcher(input).matches());
}
Prints:
ap true
apo false
AP true
GB false
gb false
gg true

Regex to identify strings containing a particular symbol?

I have set of inputs ++++,----,+-+-.Out of these inputs I want the string containing only + symbols.
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.
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").
For a sequence of the same symbol, use
(.)\1+
as the regular expression. For example, this will match +++, and --- but not +--.
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("[^\\+]*?\+[^\\+]*")

Validate string a+b

I would like to validate if the particular string is true or not in form of a + b
If input = a + b true
If input = a + false
if input = + b false
where a and b can be any string characters
I can think of a couple of ways:
Use a regex to match a "+" the characters before and after it.
Use String.indexOf("+") to find a "+" character and test the value of the index to see if it as the start or end of the string.
(Don't forget the cases where a or b could contain a "+" character; i.e. multiple "+" characters in the string.)
You can use regular expression (regex) to test the string. In java you can use the Pattern and Matcher classes to test if a string matches a given regex. The regex you want to use is:
String regex = ".* \\+ .*";
This regex will test for a string in the following form: "[characters] + [characters]".
Here is more information about the regex in java.

Writing regex for string containing no only numbers

I need to write a regex containing not only digits [0-9]. How can I do that without explicitly specifying all possible charaters in a group. Is it possible to do through lookahead/lookbehind? Examples:
034987694 - doesn't match
23984576s9879 - match
rtfsdbhkjdfg - match
=-0io[-09uhidkbf - match
9347659837564983467 - doesn't match
^(?!\\d+$).*$
This should do it for you.See demo.
https://regex101.com/r/fM9lY3/1
The negative will lookahead will check if the string doesnt have integers from start to end.You need $ to make sure the check is till end or else it will just check at the start.
If you just need to detect whether the string is not numbers-only, then you can simply test for /\D/ - "succeed if there is a non-digit anywhere".
Why not check if it only contains digits, if not it matches
String[] strings = {"034987694", "23984576s9879",
"rtfsdbhkjdfg",
"=-0io[-09uhidkbf",
"9347659837564983467"};
for (String s : strings) {
System.out.printf("%s = %s%n", s, !s.matches("\\d*"));
}
output
034987694 = false
23984576s9879 = true
rtfsdbhkjdfg = true
=-0io[-09uhidkbf = true
9347659837564983467 = false
You may try the below,
string.matches(".*\\D.*");
This expects atleast 1 non-digit character.

Java Regex - alphanumeric, allowing leading whitespace but not blank string

I've been trying to make a java regex that allows only alphanumeric characters, which can have white spaces, BUT the whole string cannot be blank...
Few examples..
" hello world123" //fine
"hello123world" //fine
"hello123world " //fine
" " //not allowed
So far I've gotten
^[a-zA-Z0-9][a-zA-Z0-9\s]*$
though this does not allow any leading whitespace and so any string with x number leading whitespace is not being matched.
Any ideas what I could add to the expression to allow leading whitespace?
How about just ^\s*[\da-zA-Z][\da-zA-Z\s]*$. 0 or more spaces at start, follow by at least 1 digit or letter followed by digits/letters/spaces.
Note: I did not use \w because \w includes "_", which is not alphanumeric.
Edit: Just tested all your cases on regexpal, and all worked as expected. This regex seems like the simplest one.
Just use a look ahead to assert that there's at least one non-blank:
(?=.*[^ ])[a-zA-Z0-9 ]+
This may be used as-is with String.matches():
if (input.matches("(?=.*[^ ])[a-zA-Z0-9 ]+")) {
// input is OK
}
You can test it using look-ahead mechanism ^(?=\\s*[a-zA-Z0-9])[a-zA-Z0-9\s]*$
^(?=\\s*[a-zA-Z0-9]) will make regex to check if at start of the string it contains zero of more spaces \\s* and then character from class [a-zA-Z0-9].
Demo:
String[] data = {
" hello world123", //fine
"hello123world", //fine
"hello123world ", //fine
" " //not allowed
};
for(String s:data){
System.out.println(s.matches("(?=.*\\S)[a-zA-Z0-9\\s]*"));
}
output
true
true
true
false

Categories