Regex to match slash does not work - java

I want a regex to match these:
/web/search/abc
/web/search/employee/999999999
/web/search/employee/78524152
But not this:
/web/search/employee/123456789
I wrote the following regex for Java, but it does not seem to be working.
/web/search/(?!/(employee/123456789)).*
Can someone tell me the correct regex to do this?

This is because you double the / in the lookahead. Try with:
/web/search/(?!employee/123456789$).*

What you tried is : /web/search/(?!/(employee/123456789)) can be represented as
You need to change it as /web/search/(?!employee/123456789) can be represented as

I would say
str.contains("^/web/search/(?!employee/123456789)")
satisfies your requirements.
Online demonstration here: http://regex101.com/r/dF1eU9

Try the following:
/web/search/(?!(employee/123456789$)).*
The slash was doubled in the negating look-ahead group.

Related

Regex to match \a574322 in Java

I have long string looking like this: \c53\e59\c9\e28\c20140326\a4095\c8\c15\a546\c11 and I need to find expressions starting with \a and followed by digits. For example: \a574322
And I have no idea how to build it. I can't use:
Pattern p = Pattern.compile("\\a\\d*");
because \a is special character in regex.
When I try to group it like this:
Pattern p = Pattern.compile("(\\)(a)(\\d)*");
I get unclosed group error even though there is even number of brackets.
Can you help me with this?
Thank you all very much for solution.
You can use this regex:
\\\\a\\d+
Code Demo
Since in Java you need to double escape the \\ once for String and second time for regex engine.
You have to change your regex to:
Pattern p = Pattern.compile("(\\\\a\\d+)");
The regex is:
(\\a\d+)
The idea is to escape a backslash and then also escape the backslash for \a, and match digits too.
You need 4 \.
2 to indicate to regex that it is not a special character, but a plain \, and 2 for each to tell the Java String that these are not special characters either. So you need to represent it in code this way:
"\\\\a\\d*"
Which is actually the regex \\a\d*
\\(a)[0-9]+ this should work
you can't try your regexps on this page or some similar
http://regex101.com/

Word that matches ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$

I am totally confused right now.
What is a word that matches: ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$
I tried at Regex 101 this 1Test#!. However that does not work.
I really appreciate your input!
What happens is that your regex seems to be in Java-flavor (Note the \\d)
that is why you have to convert it to work with regex101 which does not work with jave (only works with php, phyton, javascript)
see converted regex:
^.*(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$
which will match your string 1Test#!. Demo here: http://regex101.com/r/gE3iQ9
You just want something that matches that regex?
Here:
a1a!
This pattern matches
\dTest#!
if u want a pattern which matches 1Test#! try this pattern
^.(?=.\d)(?=.[a-zA-Z])(?=.[!##$%^&]).*$
Your java string ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$ encodes the regexp expression ^.*(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$.
This is because the \ is an escape sequence.
The latter matches the string you specified.
If your original string was a regexp, rather than a java string, it would match strings such as \dTest#!
Also you should consider removing the first .*, doing so would make the regexp more efficient. The reason is that regexp's by default are greedy. So it will start by matching the whole string to the initial .*, the lookahead will then fail. The regexp will backtrack, matchine the first .* to all but the last character, and will fail all but one of the loohaheads. This will proceed until it hits a point where the different lookaheads succeed. Dropping the first .*, putting the lookahead immidiately after the start of string anchor, will avoid this problem, and in this case the set of strings matched will be the same.

Java regex with a positive look behind of a negative look ahead

I am trying to extract from this kind of string ou=persons,ou=(.*),dc=company,dc=org the last string immediately preceded by a coma not followed by (.*). In the last case, this should give dc=company,dc=org.
Looking on regex, this seems to be a positive look behind (preceded by) of a negative look ahead.
So I have achieve this regex: (?<=(,(?!.*\Q(.*)\E))).*, but it returns ,dc=company,dc=org with the coma. I want the same thing without the coma. What I am doing wrong?
The comma appears because the capturing group contains it.
You can make the outside capture group noncapturing with (?:)
(?<=(?:,(?!.*\Q(.*)\E))).*
It seems that I have solved my problem alone, removing the capturing group around the negative look ahead. It gives the following regex: (?<=,(?!.*\Q(.*)\E)).*.
It is linked with the behavior of capturing groups in look arounds as explained here: http://www.regular-expressions.info/lookaround.html in the part Lookaround Is Atomic.

Regular expression for format XXXXXXX_YZZZZ

I am trying to write a regular expression in java which will validate following format-
XXXXXXXX_YZZZZ
where
X – alphanumeric characters(8 characters)
Y - alpha character
Z - numeric characters
what I have tried for first part is - ^[a-zA-Z0-9 ]*$ but I am not getting how to go for second part.
Can any one tell me what will be the correct regex for required format ?
You forgot to specify the amount and the underscore I assume...
/^[a-z0-9]{8}_[a-z][0-9]{4}$/i
Look at JavaDoc, then you can translate your requirements to:
"^\\p{Alnum}{8}_\\p{Alpha}\\p{Digit}{4}$"
It uses predefined character classes, like you listed in your question.
How about this?
^[a-ZA-Z0-9]{8}\_[a-zA-Z][0-9]{4}$
You can also group the results:
^([a-ZA-Z0-9]{8})\_([a-zA-Z])([0-9]{4})$
so that you can address the X, Y and Z parts individually from the results.
Try this regex:
^[A-Za-z\d]{8}_[A-Za-z]\d{4}$
Your regex matches zero or more alphanumeric characters and/or whitespaces.
This is a good place to learn regex : http://www.regular-expressions.info
Try this regular expression
^[a-zA-Z0-9]{8}[_][a-zA-Z][0-9]{4}$
Try:
^[a-zA-Z0-9]{8}_[a-zA-Z][0-9]{4}$
Regexper is your friend here.
^[a-zA-Z0-9]{8}_[a-zA-Z][0-9]{4}$
In Java, you can use metacharacters to express regulars expressions :
"8abba778_a2012".matches("^\\w{8}_[a-z]\\d{4}$");
[EDIT] : According #Jon Dvorak, I am correcting my answer. In fact, \w is too generous and also applies to the underscore character _. The correct answer :
"8abba778_a2012".matches("^[a-zA-Z0-9]{8}_[a-z]\\d{4}$");

Java regex for String:number:String?

Hi i want to know about a regex for parsing
InputDispatcherTest.TimmyTest_ASSERT
customize/base/services/input/tests/InputDispatcher_test.cpp:153: Failure
i am using \\s[:]\\d[:]\\s but it's not working.
What kind of number? Integer? Floating point? What's your input?
That doesn't look right to me. I'd do this for integers:
^\\d+$
I'd recommend trimming the String before you check.
\s matches a whitespace character, Try:
\\w+[:]\\d+[:]\\w+
I think this would suffice :
".+:\\d+:.+"
"\S+[.]\S+:\d+:[ ]\S+"
I used above Regex as a solution

Categories