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.
Related
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.".
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:
How to escape text for regular expression in Java?
(8 answers)
Closed 4 years ago.
I have searched a lot about how to ignore part of RegEx expression. Here's why.
RegEx example:
".*atan2\\(.*\\).*"
It will detect the function 'atan2' like so:
string.matches("return atan2(45, 23)")
But as I bring in another String:
string.matches("return atan2("+Integer.valueOf(XInString)+", 58)")
And if XInString looks like that: 45.7 regex will be affected of that dot, because regex will look like that then: "return atan2(45.7, 58)" and the point between 5 and 7 is a part of regex. Is there a way to ignore that part?
For example:
string.matches("return atan2([45.7], 58)")
(The string between [ and ] is what i don't want regex to read)
...the point between 5 and 7 is a part of regex. Is there a way to
ignore that part?
Yes, you can escape that dot using Pattern.quote(XInString) like this :
"return atan2(" + Pattern.quote(XInString) + ", 58)"
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:
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