Java regex matcher pattern keep returning false - java

I was having some problem with regex in Java. I have this regex pattern:
static Pattern parts = Pattern.compile("\\A([91|10|17|21|30].{1,20}\\s){1,5}\\Z");
Then I try to test it with dummy data:
String s = "91448629517150623101408002301";
Matcher testMatcher = parts.matcher(s);
System.out.println(testMatcher.matches());
String s1 = "9143676601715Sep14101310147301";
Matcher testMatcher1 = parts.matcher(s1);
System.out.println(testMatcher1.matches());
The dummy data is in the correct format. However, I not sure why both keep returns me false. Any ideas?

The regular expression you provided might be incorrect for matching the pattern. I think you might want to use \s (A whitespace character) instead of \S (A non-whitespace character).
I suggest you go through the documentation again carefully.
For example,
static Pattern parts = Pattern.compile("\\A([91|10|17|21|30].{1,20}\\S){1,5}\\Z");

Related

Java regex, replace certain characters except

I have this string "u2x4m5x7" and I want replace all the characters but a number followed by an x with "".
The output should be:
"2x5x"
Just the number followed by the x.
But I am getting this:
"2x45x7"
I'm doing this:
String string = "u2x4m5x7";
String s = string.replaceAll("[^0-9+x]","");
Please help!!!
Here is a one-liner using String#replaceAll with two replacements:
System.out.println(string.replaceAll("\\d+(?!x)", "").replaceAll("[^x\\d]", ""));
Here is another working solution. We can iterate the input string using a formal pattern matcher with the pattern \d+x. This is the whitelist approach, of trying to match the variable combinations we want to keep.
String input = "u2x4m5x7";
Pattern pattern = Pattern.compile("\\d+x");
Matcher m = pattern.matcher(input);
StringBuilder b = new StringBuilder();
while(m.find()) {
b.append(m.group(0));
}
System.out.println(b)
This prints:
2x5x
It looks like this would be much simpler by searching to get the match rather than replacing all non matches, but here is a possible solution, though it may be missing a few cases:
\d(?!x)|[^0-9x]|(?<!\d)x
https://regex101.com/r/v6udph/1
Basically it will:
\d(?!x) -- remove any digit not followed by an x
[^0-9x] -- remove all non-x/digit characters
(?<!\d)x -- remove all x's not preceded by a digit
But then again, grabbing from \dx would be much simpler
Capture what you need to $1 OR any character and replace with captured $1 (empty if |. matched).
String s = string.replaceAll("(\\d+x)|.", "$1");
See this demo at regex101 or a Java demo at tio.run

partial regex checking return false on using find method

i have a input 000.100.112 which giving false on input to the given below code,I just need partial check if exists return true
partial check is working in
String emailRegex="/^(000\\.000\\.|000\\.100\\.1|000\\.[36])/";
Pattern thePattern = Pattern.compile(emailRegex);
Matcher m = thePattern.matcher(data);
if (m.matches()) {
return true;
}
return m.find();
i expect true on partial check ,but it gives false
it gives a match in this online regex checking
Java regex patterns do not take forward slash delimiters, as they might in other languages such as PHP. Also, since you want a partial match, you should be using the following pattern:
^(000\.000\.|000\.100\.1|000\.[36]).*
^^^^ necessary
Note carefully the .* at the end of the pattern, without which a partial match won't work.
String emailRegex="^(000\\.000\\.|000\\.100\\.1|000\\.[36]).*";
Pattern thePattern = Pattern.compile(emailRegex);
Matcher m = thePattern.matcher("000.100.112");
if (m.matches()) {
System.out.println("MATCH");
}
Edit:
As #MarkMobius pointed out, you could also use your original pattern with Matcher#find():
String emailRegex="^(000\\.000\\.|000\\.100\\.1|000\\.[36])";
Pattern thePattern = Pattern.compile(emailRegex);
Matcher m = thePattern.matcher("000.100.112");
if (m.find()) {
System.out.println("MATCH");
}
Your online regex tester uses the JavaScript regex literal. In JavaScript, regexes can be delimited by /.../. Those / at the start and end that you see in the online regex tester are not actually part of the regex pattern. They are like the quote marks in a Java string.
The quote marks in "string" is not part of the string. Likewise, the slashes in /someregex/ is not part of the regex.
Therefore, when you use your regex in Java, you should not include those slashes:
String emailRegex="^(000\\.000\\.|000\\.100\\.1|000\\.[36])";
If you do, they will be interpreted as if you wanted to literally match slashes.

Why does this regex capture the excluded character?

I have a regex like this:
(?:(\\s| |\\A|^))(?:#)[A-Za-z0-9]{2,}
What I am trying to do is find a pattern that starts with an # and has two or more characters after, however it can't start in the middle of a word.
I'm new to regex but was under the impression ?: matches but then excludes the character however my regex seems to match but include the characters. Ideally I'd like for "#test" to return "test" and "test#test" to not match at all.
Can anyone tell me what I've done wrong?
Thanks.
Your understanding is incorrect. The difference between (...) and (?:...) is only that the former also creates a numbered match group which can be referred to with a backreference from within the regex, or as a captured match group from code following the match.
You could change the code to use lookbehinds, but the simple and straightforward fix is to put ([A-Za-z0-9]{2,}) inside regular parentheses, like I have done here, and retrieve the first matched group. (The # doesn't need any parentheses around it in this scenario, but the ones you have are harmless.)
Try this : You could use word boundary to specify your condition.
public static void main(String[] args) {
String s1 = "#test";
String s2 = "test#test";
String pattern = "\\b#\\w{2,}\\b";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(s1);
m.find();
System.out.println(m.group());
}
o/p :
#test
throws `IllegalStateException` in the second case (s2)..
How about:
\W#[\S]{2}[\S]*
The strings caught by this regular expression needs to be trimmed and remove the first character.
I guess you better need the following one:
(?<=(?<!\w)#)\w{2,}
Debuggex Demo
Don't forget to escape the backslashes in Java since in a string literal:
(?<=(?<!\\w)#)\\w{2,}

How to prohibit a backslash with regex in java?

I'm new with regex and just can't find what the regex is to prohibit a backslash.
Thanks for any advice.
EDIT:
I'm using the regex for JTextField, to avoid that the user writes an unvalid input. This regex currently doesnt allow the user to write a space character.
I'm doing this with
String regex = "\\S{1}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
So how I change my regex to prohibit backslash as well?
Based on your example snippet, the following expression should work similarly, but also disallowing the backslash:
String regex = "[^\\s\\\\]{1}";
It is a bit strange that you are looking for a one-char non-space and non-backslash pattern, but I guess you are iterating through and checking for consecutive matches.
I would use the following regex though:
String regex = "[^\\s\\\\]+";
and check whether it matched the whole String (matcher.matches()).
The regex pattern with java is \\\\
String somestring;
somestring = somestring.replaceAll("\\\\", "");
Would remove them. Semantically it equates down to \\ at the regex level, which becomes a literal \ match.
You can also use a Pattern match if you want to just compare, or just use String#contains
String somestring;
if (somestring.contains("\\")) {...}
To test if a string has a backslash:
if (input.matches(".*\\\\.*"))
To remove backslashes from strings:
input = input.replace("\\", "");
Note the use of the non-regex based replace() method.

How can I make a Java regex all or nothing?

I'm trying to make a regex all or nothing in the sense that the given word must EXACTLY match the regular expression - if not, a match is not found.
For instance, if my regex is:
^[a-zA-Z][a-zA-Z|0-9|_]*
Then I would want to match:
cat9
cat9_
bob_____
But I would NOT want to match:
cat7-
cat******
rango78&&
I want my regex to be as strict as possible, going for an all or nothing approach. How can I go about doing that?
EDIT: To make my regex absolutely clear, a pattern must start with a letter, followed by any number of numbers, letters, or underscores. Other characters are not permitted. Below is the program in question I am using to test out my regex.
Pattern p = Pattern.compile("^[a-zA-Z][a-zA-Z|0-9|_]*");
Scanner in = new Scanner(System.in);
String result = "";
while(!result.equals("-1")){
result = in.nextLine();
Matcher m = p.matcher(result);
if(m.find())
{
System.out.println(result);
}
}
I think that if you use String.matches(regex), then you will get the effect you are looking for. The documentation says that matches() will return true only if the entire string matches the pattern.
The regex won't match the second example. It's already strict, since * and & are not in the allowed set of characters.
It may match a prefix, but you can avoid this by adding '$' to the end of the regex, which explicitly matches end of input. So try,
^[a-zA-Z][a-zA-Z|0-9|_]*$
This will ensure the match is against the entire input string, and not just a prefix.
Note that \w is the same as [A-Za-z0-9_]. And you need to anchor to the end of the string like so:
Pattern p = Pattern.compile("^[a-zA-Z]\\w*$")

Categories

Resources