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.
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:
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
This question already has answers here:
RegEx for matching UK Postcodes
(33 answers)
Closed 7 years ago.
I'm trying to create a regex matching the following patterns (with and without space):
M1 1AA, M60 1NW, CR2 6XH, DN55 1PT, W1A 1HQ and EC1A 1BB
I'm very new at this and find it hard to create a functional regex for all the examples above.
Searching here and there I found a regex that might work for some of the patterns but I don't know how to add the condition "with or without space" for each type of postcode.
Here the regex I found on another post "^(A-PR-UWYZ [0-9][ABD-HJLNP-UW-Z]{2})"
How do I add the space/no space condition? In order to match M11AA or M1 1AA.
You need this regex:
^([A-PR-UWYZ](([0-9](([0-9]|[A-HJKSTUW])?)?)|([A-HK-Y][0-9]([0-9]|[ABEHMNPRVWXY])?)) ?[0-9][ABD-HJLNP-UW-Z]{2})$
^
This space must be set as optional with ? quantifier that means 0 or 1 repetition.
See demo