This question already has answers here:
Regex plus vs star difference? [duplicate]
(9 answers)
Closed 2 years ago.
I am expecting the following regex to allow 0-n spaces between operators, but it is forcing at least one. Can someone please correct the error of my ways?
((\d+\.?\d*|\d*\.?\d+\s+?[\+\-\/\*]\s+?)+)(\d+\.?\d*|\d*\.?\d+)
Examples
24*3.2
24 * 3.2
Only the 2nd example is allowed through.
I understood \s+? should be an optional number of spaces?
Playpen
+? (or more generally ? following any other quantifier) is a non-greedy quantifier. It does not mean “make the preceding match optional”.
Use * instead of +?.
What you need is \s*, i.e., 0 or more white spaces.
Your \s+? says 1 or more white spaces and non greedy match for spaces.
The ? modifier after * or + means non-greedy match.
Related
This question already has answers here:
Regex that matches anything except for all whitespace
(5 answers)
Closed 2 years ago.
I'm using a regular expression to match Facebook url.
((http|https)://)?(www[.])?facebook.com/.+
It accepts:
facebook.com/xxxxx
www.facebook.com/xxxxx
http://facebook.com/xxxxx
https://facebook.com/xxxxx
But it still accepts whitespaces after /:
facebook.com/(spaces there)
How can I prevent it?
You can shorten the pattern by making the s optional in https using a quesion mark, and use \S+ to match 1 or more non whitespace characters instead of .+ which can also match spaces.
(?:https?://)?(?:www\.)?facebook\.com/\S+
Regex demo
This question already has answers here:
What is a non-capturing group in regular expressions?
(18 answers)
Closed 2 years ago.
I have this regexp:
^(?<FOOTER_TYPE>[ a-zA-Z0-9-]+)?(?<SEPARATOR>:)?(?<FOOTER>(?<=:)(.|[\r\n](?![\r\n]))*)?
Which I'm using to match text like:
BREAKING CHANGE: test
my multiline
string.
This is not matched
You can see the result here https://regex101.com/r/gGroPK/1
However, why is there the last Group 4 ?
You will need to make last group non-capturing:
^(?<FOOTER_TYPE>[ a-zA-Z0-9-]+)?(?<SEPARATOR>:)?(?<FOOTER>(?<=:)(?:.|[\r\n](?![\r\n]))*)?
Make note of:
(?:.|[\r\n](?![\r\n]))*)?
(?: at the start makes this optional group non-capturing.
Updated Demo
it is group 4 because the fourth parentheses you defined is:
(.|[\r\n](?![\r\n]))*)
it translate to
"either dot, or the following regex"
and in the example you have, it ends on a dot.
string.
so as regex is usually greedy, it captures dot as the forth group
This question already has an answer here:
Regex Match a character which is not followed by another specific character
(1 answer)
Closed 4 years ago.
I have the following situation:
3" a
3":a
3",a
3"a
3"2
3"A
I need to find a replace a double quote with space every time the double quote is not following by : or ,.
So, for my case the expected results will be:
3 a
3":a
3",a
3 a
3 2
3 A
Any idea how write this logic using regex?
Regards,
You can use a negative lookahead A(?!B) for that. It matches an expression A that is not followed by expression B.
The replacement of the matches with spaces will depend on the used language.
"(?![:,])
Applied to your examples: https://regex101.com/r/UiPlaC/2
If you want to handle the case 3" a without having multiple spaces, just include one (or even more?) optional spaces in the match.
"(?![:,])\ ?
See here for more information:
Regex lookahead, lookbehind and atomic groups
https://www.regular-expressions.info/lookaround.html
This question already has answers here:
Difference between regex [A-z] and [a-zA-Z]
(6 answers)
Closed 8 years ago.
I'm struggling with the following regexp
[A-z0-9]+
If tested against this string:
||a919238[.--a]asd|
it returns a919238[, including the square bracket.. I tried to input my test case on regex101 to understand what's wrong, but the site regex explanation is not helping, probably I'm not able to see my mistake.
Why is the square bracket included in the result?
Because
[A-z0-9]+
↑ ↑
is from A to z, see the ASCII table, ] appears between the two characters:
A===>64
z===>122
[===>91
So it is in between the range you have defined.Use [A-Za-z0-9]+
You can use /[a-z0-9]+/i (the i makes it case-insensitive), or /[A-Za-z0-9]+/.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
What is the difference between .* and (.*) in regular expressions?
From what I've seen,
AB.*DE
and
AB(.*)DE
appear to match the same things but I want to know if there are any differences so I use the correct one.
I need to be able to match any number of characters between AB and DE and even match if there isn't anything between them (ABDE).
If .* and (.*) mean the same thing, is there a "better" one to use in terms of standards/best practice?
.* Matches any character zero or more times.
(.*) - Matched characters are stored into a group for later back-referencing(any charcter within () would be captrued).
AB.DE Matches the string ABanycharDE. Dot represent any character except newline character.
AB(.)DE AB and DE are matched and the in-between character is captured.
The parentheses indicate a capture group.
There is no difference. Both will match any character zero+ times. However, the capture group is considered better because it allows you to group together your conditions. This makes your regular expressions look nicer and more readable just like parenthesis in math equations make the equation look nicer.