A pattern that matches all except starting with a word [duplicate] - java

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 2 years ago.
How can I set a regex pattern which matches all words but the strings which starts with
/word
/word/
/word/ following by anything else.
I think the pattern starts with \A but I don'0t know how to tell that should not follow a word
Thanks

Use this kind of negate regex and replace word by your word.
^((?!word).)*$

You can use the regex:
^(?!\\/word).*$
See it

Take a look at the lookaround feature offered by regular expressions. Also, a similar thread. Also, posting your question in terms of a concrete sample problem might help you get a few working sample snippets.

Perhaps using a string comparison will be clearer and faster.
if (text.startsWith("word")) {
// text is OK
} else {
// not OK
}

Related

Java regular expression match end doesn't work [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 2 years ago.
I want to match the last character "c". What am I doing wrong? In the documentation it's clearly explained that $ matches the end of the line, i have used regex milions of time on unix shell ecc... and always worked as expected but in java no.
String string = "abc";
if(string.matches("c$")){//I know that .*c$ will work.
System.out.println("yes");//This is never printed
}
Where is the error?
I know that .*c$ will work, but by reading the javadoc I can't find this information.
Can some one tell me how do I interpret what is the meaning of this java official tutorial?
https://docs.oracle.com/javase/tutorial/essential/regex/bounds.html
or this?
https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum
Am I not able to read? Because it seems pretty obvious, but I really can't find the solution, I fell really retarded in this moment!
Under java.lang.String there is a method who clearly say the following:
matches(String regex)
Tells whether or not this string matches the given regular expression.
This will print YES.
String line = "abc";
Pattern pattern = Pattern.compile("c$");
Matcher matcher = pattern.matcher(line);
System.out.println(matcher.find() ? "YES" : "NO");

Replacing Regular expression matches in Java [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.

Regular expression with captured groups for parsing a range of two real numbers [duplicate]

This question already has answers here:
simple java regex throwing illegalstateexception [duplicate]
(3 answers)
Closed 5 years ago.
I need a regex to properly parse ranges of two real numbers (unsigned), presented with a hyphen.
Valid inputs:
1-3
3.14-7.50
0-4.01
It's Java on Android.
My current approach:
Pattern pattern = Pattern.compile("(?<Minimum>\\d+(\\.\\d+))-(?<Maximum>\\d+(\\.\\d+))");
Matcher matcher = pattern.matcher("3.14-5.2");
String min = matcher.group("Minimum");
String max = matcher.group("Maximum");
It crashes on attempting to retrieve the minimum.
java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.getMatchedGroupIndex(Matcher.java:1314)
at java.util.regex.Matcher.group(Matcher.java:572)
I can't really see what's wrong with the expression.
I would particularly appreciate an explanation on what the problem with it is. A regex allowing for optional white space around the hyphen would be extra nice, too (I'd like it to work that way but I dropped this for now as I can't get it to work at all).
You need to make decimal part optional:
Pattern pattern = Pattern.compile(
"(?<Minimum>\\d+(?:\\.\\d+)?)-(?<Maximum>\\d+(?:\\.\\d+)?)");
? after (?:\\.\\d+) will make that group an optional match
Better to use ?: for making it a non-capturing group
Also you need to call matcher.find() or matcher.matches() before calling .group(int) method.

What's wrong with this pattern? [duplicate]

This question already has answers here:
What is the difference between the two regex pattern
(2 answers)
Closed 7 years ago.
Can anybody please tell me what's wrong with this regexp
"^[a-zA-Z0-9 -\\/_&()']*$"
I expect this to accept only values like abc123/-_'s but I'm not sure why it's even accepting ABC
But it's not accepting double quotes in it.
Here is my code:
public static final Pattern
PATTREN = Pattern.compile("^[a-zA-Z0-9 -\\/_&()']*$");
Matcher m = PATTREN .matcher("ABC\"");
return m.matches();
I believe this is what you want
Only character that needs escaping within [] is the hyphen - which has special meaning. Everything else is literal, even brackets, slashes etc. which usually have meaning.
No need for start and end markers
You can use Pattern.CASE_INSENSITIVE flag in pattern rather than added extra complexity via A-Za-z
Code
Example
Pattern pattern = Pattern.compile("[a-z0-9\\-/_&()']*", Pattern.CASE_INSENSITIVE);
System.out.println(pattern.matcher("abc123/-_'s").matches());
System.out.println(pattern.matcher("ABC\"").matches());
The pattern says, A-Z, so why do you think it would not allow uppercase characters?
You are defining a character class, using []; and anything between the square brackets is seen as "valid" character. So if you dont want uppercase letters, then remove A-Z from the [].
Btw: although it is not supporting Java, you might want to play with https://regex101.com/
That is a great site to "practice" regular expressions; and it isnt too hard to "convert" regexes from other languages to Java.

Regex, trim multiple characters? [duplicate]

This question already has answers here:
Removing repeated characters in String
(4 answers)
Closed 8 years ago.
Lets say I have a string:
tttteeeeeeessssssttttttt
Using the power of regex, how can that string be turned into:
test
At first look it seems easy to do, but the current code (not regex) I have for it is not behaving well and im pretty sure regex is the way to go.
You can use:
str = str.replaceAll("([A-Za-z])\\1+", "$1");
RegEx Demo
Use string.replaceAll function.
strng.replaceAll("(.)\\1+", "$1");
The above regex captures the first character in the sequence of same characters and matches all the following one or more characters (which must be same as the one inside the capturing group) . Replacing those characters with the character inside group index 1 will give you the desired output.
Example:
System.out.println("tttteeeeeeessssssttttttt".replaceAll("(.)\\1+","$1" ));
Output:
test
(.)(?=\1)
Try this.Replace by empty string.See demo.
https://regex101.com/r/tX2bH4/41
str = str.replaceAll("(.)(?=\\1)", "");

Categories