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
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:
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:
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.
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:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
I m trying regex to get the strings
starting with # and
not ending with a dot(.)
For that i tried the java code(link here) but this does not show any results -
#(\\w+)*(?<!.(.))*$
The string i m trying is -
This is a test\nAnother #pradyut#test ht#html.com\ntest\n#art\n#cool#paintings#collections
This should return
pradyut
test
cool
The result html ending with a .com should not return.
Regards
You can use this regex:
(?<=#)\w+\b(?!\.)
In Java you have to use:
(?<=#)\\w+\\b(?!\\.)
Regex Demo