String#replaceFirst not working seemingly randomly [duplicate] - java

This question already has answers here:
Not able to replace all for dollar sign
(4 answers)
Closed 3 years ago.
I have a string, which I try to replace the first appearance of %s.
For example:
"$%s".replaceFirst("%s", "10");
returns $10, but
"&cYou do not have %s!".replaceFirst("%s", "$10");
throws java.lang.IndexOutOfBoundsException: No group 1
I have print statements, I know 100% that is what is throwing the error, and that is what is going into the statement.

The replacement containing a $ prefix is considered as group matcher (i.e. it tries to replace only matching group.
So the $ needs to be escaped with \\:
System.out.println("&cYou do not have %s!".replaceFirst("%s", "\\$10"));

Related

Java regex match part of path [duplicate]

This question already has answers here:
Regular expression to match a backslash followed by a quote
(3 answers)
Escaping special characters in Java Regular Expressions
(7 answers)
How to check if a directory is inside other directory
(1 answer)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
I am trying to check if a chosen path is a valid path for my Java program. In order to be valid, it must match the path E:\test\(someFolderName)\. The chosen folder can be deeper in that directory.
This is what I have tried:
String a = "E:\\test\\anotherFolder";
if (a.matches("E:\\\\btest\\b\\.*")) {
System.out.println("match");
}
I have also tried putting test into [] but it did not work.
\b would mark the beginning of a word boundary, and adding \b again should close it, correct?
.* would match any character 1 to infinite times.
So, is there a problem with the escaping? Or do I need to group it differently?
Possible duplicated of escaping-special-characters-in-java-regular-expressions.
You need more backslashes. "The 4 slashes in the Java string turn into 2 slashes in the regex pattern. 2 backslashes in a regex pattern matches the backslash itself.".

Regular expression to match a word starts and ends with a combination of special characters [duplicate]

This question already has answers here:
Java - Best way to grab ALL Strings between two Strings? (regex?)
(3 answers)
Closed 4 years ago.
I am trying to write a regular expression which gives me words which starts with <!= and ends with =>. For example if there is a sentence what is your <!=name=>, the result should give me name because it matches my pattern.
I have read to use this ^ for starts with and $ for ends with, but I am not able to match a combination of special characters.
As in the comment. You can use <!=(\w+)=> because the exclamation mark and equal sign are not part of word-character class you can simply test for those characters and match the word characters between them. check:https://regex101.com/r/qDrobh/4
For multiple words you can use:<!=((?:\w+| )*)=>
See:https://regex101.com/r/qDrobh/5

Java regex not respecting the specified quantifier [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
I have the following line from a CSV file:
Name,Age,Country,State,Zip,Phone,Email,Address
I am using the following Java regex to capture Name,Age,Country into 1 group but it always captures this:
Regex --> ^((?:.*,){3})
Result --> Name,Age,Country,State,Zip,Phone,Email,
Why is it not respecting the {3} quantifier I am using?
A dot matches a comma too. You have two solutions:
the bad one, make it not greedy: ^((?:.*?,){3})
the right one: exclude commas: ^((?:[^,]*,){3})
The first one is bad because it's expensive and has potential for catastrophic backtracking.

Java regex matching with or clause [duplicate]

This question already has answers here:
Java regular expressions and dollar sign
(5 answers)
Closed 6 years ago.
I've got the following code
String valuepairName = "sc_mpl_MAX_AUD_TIME_To_20_Val.$$DATE_TO_LOAD";
boolean result = valuepairName.matches("sc_mpl_MAX_AUD_TIME_To_(20|32|82)_Val.$$DATE_TO_LOAD");
The result evaluates to false, but I cannot see the mistake.
This must be very trivial, hence it's driving me crazy :(
You need to escape each $ individually (or use pattern.quote) ($ is a special character in regex). Use
boolean result = valuepairName.matches("sc_mpl_MAX_AUD_TIME_To_(20|32|82)_Val.\\$\\$DATE_TO_LOAD");

Stripping specific chars from beginning/ending of a string [duplicate]

This question already has answers here:
Regex to trim hyphens from start and end of a string
(2 answers)
Closed 7 years ago.
Given a word-string in Java, I want to strip off from beginning and from end, exactly these specified set of characters:
[?:!.,;'\"«»]
as many times as they appear.
For instance, «Be!!» should become just Be, "Here!!!" should become Here, «I should become I.
Can anyone provide a correct way to do this?
Use an anchored regex in string.replaceAll function.
string.replaceAll("^[?:!.,;'\"«»]+|[?:!.,;'\"«»]+$", "");
DEMO

Categories