Convert regex into java regex [duplicate] - java

This question already has answers here:
Regexp Java for password validation
(17 answers)
Closed 5 years ago.
string should not be longer than 26 alphanumeric characters
string should not begin with www OR api OR admin
string may contain hyphens
I have this regular expression that works:
^(?!www)(?!admin)(?!api)[a-zA-Z0-9.]{1,26}
Can you help me convert that regex into a java style string regex?

I found the answer by changing my regex to the following:
^(www|api|admin)\w{1,26}$

Related

java Complex Regex doesn't work in String.matches() [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 4 years ago.
i have this regex which i want to check if string do contain this structure
none of the strings below match , why?
String s = "T4018.test.12"
or
String s = "T4018.foo.32"
or
String s = "develop"
if(s.matches("T40[0-9][0-9][.][a-zA-Z]+|develop"))
{
//Never matches
}
its do match :
https://regex101.com/r/CSLugb/1
UPDATE
Solved it using Pattern.compile
Maybe you can rewrite the RegEx to "T40\\d{2}\\.\\w+\\.\\d{2}|develop" for matching more exactly. See your 3 test-cases matched in Java

Java Regular Expression - Match for anything but specific string [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 6 years ago.
How would I write a Java regular expression that matches everything except the following string?
abc123DEF#domain.com
(?!abc123DEF#domain.com) doesn't work.
Just a simple change of your regex:
(?!abc123DEF#domain.com).*
it works.

Java : Remove strange special characters from String [duplicate]

This question already has answers here:
How can non-ASCII characters be removed from a string?
(10 answers)
Closed 6 years ago.
I want to remove all strange special characters from a string in Java.
Those strange special characters are appearing in form of ?(Question mark) in MS Word.The image of sample string is given below.
You can use
String newString = my_string.replaceAll("\\p{C}", "");
more information about Java Unicode Regular expression Java Unicode Regular expression here
This will work:
String string = yourString.replaceAll("[^\\x00-\\x7F]", "");

Regex too greedy Java [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 7 years ago.
I'm trying to retrieve some parts from a single line html string.
The Regex I wrote is as follows:
String pattern = "rel=\"bookmark\">(.*?)</a></h2>"
The html line is like:
....rel="bookmark">what I need</a></h2>....rel="bookmark">....rel="bookmark">What I also need</a></h2>....
My Regex has two results:
rel="bookmark">what I need</a>
and
rel="bookmark">....rel="bookmark">What I also need</a></h2>
How can I tell it to be less greedy for the second result?
Between tags are the things does not contains < and >
String pattern = "rel=\"bookmark\">[^<>]+</a>";

java regex to not get string not ending with dot [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I m trying regex to get the strings
starting with # and
not ending with a dot(.)
For that i tried the java code(link here) but this does not show any results -
#(\\w+)*(?<!.(.))*$
The string i m trying is -
This is a test\nAnother #pradyut#test ht#html.com\ntest\n#art\n#cool#paintings#collections
This should return
pradyut
test
cool
The result html ending with a .com should not return.
Regards
You can use this regex:
(?<=#)\w+\b(?!\.)
In Java you have to use:
(?<=#)\\w+\\b(?!\\.)
Regex Demo

Categories