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.".
Related
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.
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
This question already has answers here:
Regular expression to match balanced parentheses
(21 answers)
Closed 5 years ago.
I have found some regex to match textblocks between brackets. However, what if I have some String with nested brackets and I only want the most external part of it.
eg. "foo bar [first [second] [third]] asdf ]]]]"
I would like to be able to match the text between the first opening bracket and it's closing bracket, leaving everything inside intact.
Result would be: "[first [second] [third]]"
The classical recursion problem (if recursion is supported):
\[(?:[^][]*|(?R))*\]
See a demo on regex101.com.
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.
This question already has answers here:
How to use beginning and endline markers in regex for Java String?
(5 answers)
How to use java regex to match a line
(2 answers)
Closed 5 years ago.
given the following expression:
Pattern.compile("^Test.*\n").matcher("Test 123\nNothing\nTest 2\n").replaceAll("foo\n")
This yields:
"foo\nNothing\nTest 2\n"
for me. I expected that the last line is also replaced to foo\n since there is a linebreak immediately before Test 2 in the input string.
Why is doesn't the regex match there?
You have to add the multiline flag to the pattern: Pattern.MULTILINE.
Pattern.compile("^Test.*\n", Pattern.MULTILINE).matcher("Test 123\nNothing\nTest 2\n").replaceAll("foo\n")
By Default the match is only single line. For more Informations see the javadoc
At the beginning of your regex you have a ^ sign which normally anchors the regex to the beginning of a tested string. You need to specify multiline regex option (Oracle Documentation link) to make it apply to start of each line instead.
Try this (I have split the lines for legibility, feel free to oneline it back):
Pattern.compile("^Test.*\n", Pattern.MULTILINE)
.matcher("Test 123\nNothing\nTest 2\n")
.replaceAll("foo\n")
Unfortunately I do not have Java environment set up at the moment, so I'm unable to check this by myself.