Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to write a regex that will find everything except for '.' - that is, a string which only contains '.' should return false and everything else true.
String regex = "(?!(^\\.$)).*";
String test = ".";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(test);
System.out.println(matcher.find());
^(?![.]$).*$
This should do it for you.You need to use anchors to make sure you dont make any partial matches.
I think this will do the job [^.].*|[.].+, i tried 15 different inputs on http://www.regexplanet.com/advanced/java/index.html, and it only says false when trying to find '.'
Explanation:
[^.].* - match everything that starts with no-dot character, and after that has 0 or more of any other characters,
[.].+ - match everything that starts with dot, and is followed by at least one or more of any other characters.
[^.].*|[.].+ - both parts merged with operator 'or'
Do you really need a regex for this, just use string comparison:
if (!".".equals(test)) {
// not equal to a dot
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm working on a regex for getting a specific number pattern from the URL string.
Requirements: Desire string should start from - or /, followed by a sequence of digits and ending with a / or nothing.
I tried: [-\/](\d+)(\/|$), but for e.g. in www.abc.com/pages/Toms-1777/14623420046 I want /14623420046(i.e. the second occurring digit sequence), but according to my regex, the result will be -1777/. I was trying negative lookbehind but not able to make any progress. I'm new to all this. Please guide.
Test cases: (with matched pattern)
www.abc.com/pages/Essen-Massage-Therapy-LLC/130561253629638
www.abc.com/biz/finn-mccools-santa-monica-2
www.abc.com/summerset.gardens.7
www.abc.com/pages/Toms-1777/14623420046
www.abc.com/pages/The-Clean-Masters/1403753595526512
www.abc.com/24hfsheepsheadbay
www.abc.com/sample2NVCoolSpace
www.abc.com/pages/Jet-Set-3920/542495615847409
www.abc.com/temp.buildings.77
www.abc.com/2423423453534temp/2312312312312312312
www.abc.com/Ptemp-Gtemp-Dtemp-189398324428792/temp
You want that $ in either case. Instead of 'slash OR end', it's more 'optional slash and then a very much not-optional end'. So.. /?$. You don't need to normally escape slashes, especially in java regexes:
Pattern.compile("[-/](\\d+)/?$")
This reads: From minus or slash, a bunch of digits, then 0 or 1 slashes, then the end. Note, use find() and not matches() - matches only works if the entire string matches, which it won't, as the - or / occurs halfway through.
EDIT: Was missing a backslash in the java string.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
In a Java app, I use this regex: (\w+)_\d to match patterns of this form:
apples_1
oranges_2
and then I use the first capturing group value (apples, oranges).
However, I now have a new request to also match these strings:
applesdrp_1
orangesdrp_2
where 'drp' is a fixed 3 character string, and the same values as before need to be captured: apples, oranges
So for example, if I use this regex: (\w+)(?:drp)?_\d
it will do the work on apples_1, but not for applesdrp_1.
Is there a way to do that with a regex?
You can use a non-greedy quantifier:
(\w+?)(?:drp)?_\d
In this way \w+? will take characters until it find "drp_N" or "_N" (where N is a digit).
If you use a greedy quantifier, \w+ takes all possible character (including the underscore and the digit since they are included in \w) and then gives back characters one by one until (?:drp)?_\d succeeds. But since (?:drp)? is optional, the regex engine stops to backrack when it find _N.
Yes, you can - one way would be using a negative lookbehind, to make sure, that the drp is forced outside the group, if it is present
(\w+)(?<!drp)(?:drp)?_\d+
See https://regex101.com/r/jJ1rM4/3 for a demo
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need to validate and prevent entering '='(equal sign) for an input string and i use regex to catch it [=]* . but it catches the other strings as well. ex input: 2c450807-4a4c-4f18-bf4f-5a100ced87a0 . above regex catches the this string as well.
please help me.
and also ,can anyone please explain me why this regex doesn't catch the input. I need to catch the special characters mentioned in the regex.
final String REGEX="[.,%*$##?^<!&>'|/\\\\~\\[\\]{}+=\"-]*";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher("2c450807-4a4c-4f18-bf4f-5a100ced87a0");
if (matcher.matches()) {
System.out.println("found");
}
else{
System.out.println("not found!");
}
this prints "not found!"
When you use regular expression, you might want to find items depending the number of times they appear:
If you want to match a group containering exactly n symbol (in your case: Equal (=) ) you can do something like this:
(=){n}
ie: if(myVar === myValue) is matched when n=3
If you want to match this symbol One or More times:
(=)+
ie: if((myVar = myValue) or (myVar == myValue) or (myVar === myValue))
If you want to match an item which might appear:
(=)*
ie: if(myVar < myValue)
The item does not need to be present in your expression to check. The value can be present 0 to n times.
I think the problem you have is that the * quantifier allows 0 occurrences of the preceding subpattern. Thus, [=]* matches any string.
You need to use a mere
=
And then you will not match 2c450807-4a4c-4f18-bf4f-5a100ced87a0.
Also please note that = is not a special regex character, you do not need to escape it, or place into a character class to avoid escaping.
However, as it has been pointed out in another comment, if you do not have to use a "regex", just check if a string contains = with a str.contains("=").
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I tried creating a regex which will help me solve the below problem but was not able to:
Example word : mumbai(10)
The regex should check if the word is ending with parenthesis with a value I.e (10) in the above word "Mumbai(10)". And the value should be extracted I.e 10.
If the word is Mum(10)bai then no results should be the output.
You can use the following to match:
\\((\\d+)\\)$
And extract group 1 $1 for the value.
See DEMO
Java code:
String str = "mumbai(10)";
Matcher m = Pattern.compile("\\((\\d+)\\)$").matcher(str);
if (m.find()) {
System.out.println(m.group(1));
}
String string = "mumbai(10)";
Pattern p = Pattern.compile("^.*\\((\\d+)\\)$");
Matcher m = p.matcher(string);
if (m.matches()) {
System.out.println(m.group(1));
}
^ and $ denote the beginning and end respectively. .* just matches anything. The parenthesis need to be escaped, since they represent a group in regular expression, which is being made use of after that. The captured group can then be extracted using m.group(1).
You can try following regex,
\((\d+)\)$
Demo
It uses the concept of group capture
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
As the title states, i want to know how i can check if a string consists of 2 integers with a blank space in between them in Java.
As an example:
0 2, should return true.
0 abc, should return false.
abcsd, should return false.
And so on...
If it is to any help, I am getting my string from a text file with a buffered reader. Maybe there is a more direct and easier way?
Thank you in advance
You could use string.matches method.
string.matches("\\d+\\s\\d+");
DEMO
\d+ matches one or more digits. So this asserts that the input string must contain a number at the start.
\s matches a space character. So this asserts that the input string must contain a space at the middle.
\d+ matches one or more digits. So this asserts that the input string must contain a number at the end.
Since matches method tries to match the whole input string, you don't need to add start and end anchors to your regex.
Because you haven't posted your own code here, I assume that you haven't made much research into it, have you? First of all, will you use only 1-digit numbers? Here's how you should start: http://docs.oracle.com/javase/tutorial/java/data/converting.html