I have :
^-?[0-9]\d*(\.\d+)?$
But need it to allow only up to 3 decimal places. So allowed values are:
+10.123
-10.123
10.123
10
+10
-10
10.1
10.12
Not allowed:
10.1234
10.123%
Advice / suggested expression mods appreciated.
Thanks in advance.
In addition to * and + metacharacters, which specify unlimited repetition, regex allows you to place specific limits on the number of matches with the {a,b} construct. Here, a is the minimum required number of matches, and b is the maximum. Both a and b are inclusive.
Since you need to match at least one and at most three digits, you need to replace \d+ with \d{1,3}:
^[+-]?[0-9]\d*(\.\d{1,3})?$
Optimization: With a working regex in hand, you can optimize by replacing [0-9] with another \d, and "folding" it into \d* by using \d+:
^[+-]?\d+(\.\d{1,3})?$
^[+-]?\d+(\.\d{1,3})?$
Explanation:
See it here: https://www.debuggex.com/r/BbCBL5pQWLxsD4a6
^ asserts position at start of a line
Match a single character present in the list below [+-]?
? Quantifier — Matches between zero and one times, as many times as possible,
giving back as needed (greedy)
+- matches a single character in the list +- (case sensitive)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible,
giving back as needed (greedy)
1st Capturing Group (\.\d{1,3})?
? Quantifier — Matches between zero and one times, as many times as possible,
giving back as needed (greedy)
\. matches the character . literally (case sensitive)
\d{1,3} matches a digit (equal to [0-9])
{1,3} Quantifier — Matches between 1 and 3 times, as many times as possible,
giving back as needed (greedy)
$ asserts position at the end of a line
Explanation From: [https://regex101.com/]
^[+-]{0,1}\d*?(\.{0,1}\d{0,3})?$ should work
see https://regex101.com/r/P6DBrW/1/ for Explanation of the regexp
^(?!0\d)\d*
(\.\d{1,4})?$
Related
I have several files where I am trying to extract the second group of numbers:
Length 1.2345 +- 0.765
I am trying to extract 0.765 or what the number is.
I have tried many regex combinations. I either extract 1.2345 +- 0.765 or nothing.
Any suggestions?
Thanks,
Dave
You can find a group of numbers or . that come after a first group of numbers or .. and use regex capture groups. See regex101.com to test your regex. For example, here's one way to find a pattern of: no number 0 or more times, then number or decimal 1 or more times, then no number or decimal, then second number:
text = 'Length 1.2345 +- 0.765'
number2 = re.sub('[^0-9\.]{0,}[0-9\.]{1,}[^0-9\.]{1,}([0-9\.])', r'\1', text)
print(number2)
# 0.765
this is another regex.
(?=[^-+])(\d*\.?\d+)$
1st Capturing Group (\d*.?\d+)
\d matches a digit (equivalent to [0-9])
matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
. matches the character . literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
and here is it on regex101
https://regex101.com/r/VV6prY/1
This question already has answers here:
How do I include negative decimal numbers in this regular expression?
(17 answers)
Closed 3 years ago.
I need some help coming up with a Regex that allows a decimal value between -100 and 100.
It can have up to two decimal places but decimals should not be required.
I have this for 0 - 100 based on this question:
Regex range between 0 and 100 including two decimal
^(?:100(?:\.00?)?|\d?\d(?:\.\d\d?)?)$
You can try this expression :
/^-?(?:100|[0-9]{1,2})(?:\.[0-9]{1,2})?$/gm
^ asserts position at start of a line
-? matches the character - literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy) (negatives are optional)
Non-capturing group (?:100|[0-9]{1,2}) (hundred or less than)
1st Alternative 100 matches the characters 100 literally (case sensitive)
2nd Alternative [0-9]{1,2} matches a single character present in the list [0-9]
{1,2} Quantifier — Matches between 1 and 2 times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 and 9 (case sensitive)
Non-capturing group (?:\.[0-9]{1,2})? (decimals not required)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
\. matches the character . literally (case sensitive)
[0-9]{1,2} matches a single character present in the list [0-9]
{1,2} Quantifier — Matches between 1 and 2 times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 and 9 (case sensitive)
$ asserts position at the end of a line
Global pattern modifier/flags :
g : global. All matches (don't return after first match)
m : multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
What is the output of this regular expression?
/\s+(\d+)\s+/
In particular what is the meaning of /\s
In your regex \s+ matches any number of whitespaces sequentially and /d+ matches any number of digits sequentially .
\s and \d matches a single whitespace and single digit respectively the + makes it match any number of sequential whitespaces and digits respectively.
You can find a full explanation at regex101.com.
/\s+(\d+)\s+/
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
https://regex101.com/
might be useful :D
\s+(\d+)\s+ / ↵ matches the character
↵ literally (case sensitive)
\s+
matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) 1st Capturing Group
(\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s+ matches any whitespace
character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed
I need a regular expression that will tell if a string is in the following format. The groups of numbers must be comma delimited. Can contain a range of numbers separated by a -
300, 200-400, 1, 250-300
The groups can be in any order.
This is what I have so far, but it's not matching the entire string. It's only matching the groups of numbers.
([0-9]{1,3}-?){1,2},?
Try this one:
^(?:\d{1,3}(?:-\d{1,3})?)(?:,\s*\d{1,3}(?:-\d{1,3})?|$)+
Since you didn't specify the number ranges I leave this to you. In any case you should do math with regex :)
Explanation:
"
^ # Assert position at the beginning of the string
(?: # Match the regular expression below
\\d # Match a single digit 0..9
{1,3} # Between one and 3 times, as many times as possible, giving back as needed (greedy)
(?: # Match the regular expression below
- # Match the character “-” literally
\\d # Match a single digit 0..9
{1,3} # Between one and 3 times, as many times as possible, giving back as needed (greedy)
)? # Between zero and one times, as many times as possible, giving back as needed (greedy)
)
(?: # Match the regular expression below
# Match either the regular expression below (attempting the next alternative only if this one fails)
, # Match the character “,” literally
\\s # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\\d # Match a single digit 0..9
{1,3} # Between one and 3 times, as many times as possible, giving back as needed (greedy)
(?: # Match the regular expression below
- # Match the character “-” literally
\\d # Match a single digit 0..9
{1,3} # Between one and 3 times, as many times as possible, giving back as needed (greedy)
)? # Between zero and one times, as many times as possible, giving back as needed (greedy)
| # Or match regular expression number 2 below (the entire group fails if this one fails to match)
\$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
)+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"
^(\d+(-\d+)?)(,\s*(\d+(-\d+)?))*$
This should work:
/^([0-9]{1,3}(-[0-9]{1,3})?)(,\s?([0-9]{1,3}(-[0-9]{1,3})?))*$/
You need some repetition:
(?:([0-9]{1,3}-?){1,2},?)+
To ensure that the numbers are correct, i.e. that you don't match numbers like 010, you might want to change the regex slightly. I also changed the range part of the regex, so that you don't match things like 100-200- but only 100 or 100-200, and added support for whitespaces after the comma (optional):
(?:(([1-9]{1}[0-9]{0,2})(-[1-9]{1}[0-9]{0,2})?){1,2},?\s*)+
Also, depending on what you want to capture, you might want to change the capturing brackets () to non capturing ones (?:)
UPDATE
A revised version based on the latest comments:
^\s*(?:(([1-9][0-9]{0,2})(-[1-9][0-9]{0,2})?)(?:,\s*|$))+$
([0-9-]+),\s([0-9-]+),\s([0-9-]+),\s([0-9-]+)
Try this regular expression
^(([0-9]{1,3}-?){1,2},?\s*)+$
I have a URL, and I'm trying to match it to a regular expression to pull out some groups. The problem I'm having is that the URL can either end or continue with a "/" and more URL text. I'd like to match URLs like this:
http://server/xyz/2008-10-08-4
http://server/xyz/2008-10-08-4/
http://server/xyz/2008-10-08-4/123/more
But not match something like this:
http://server/xyz/2008-10-08-4-1
So, I thought my best bet was something like this:
/(.+)/(\d{4}-\d{2}-\d{2})-(\d+)[/$]
where the character class at the end contained either the "/" or the end-of-line. The character class doesn't seem to be happy with the "$" in there though. How can I best discriminate between these URLs while still pulling back the correct groups?
To match either / or end of content, use (/|\z)
This only applies if you are not using multi-line matching (i.e. you're matching a single URL, not a newline-delimited list of URLs).
To put that with an updated version of what you had:
/(\S+?)/(\d{4}-\d{2}-\d{2})-(\d+)(/|\z)
Note that I've changed the start to be a non-greedy match for non-whitespace ( \S+? ) rather than matching anything and everything ( .* )
You've got a couple regexes now which will do what you want, so that's adequately covered.
What hasn't been mentioned is why your attempt won't work: Inside a character class, $ (as well as ^, ., and /) has no special meaning, so [/$] matches either a literal / or a literal $ rather than terminating the regex (/) or matching end-of-line ($).
/(.+)/(\d{4}-\d{2}-\d{2})-(\d+)(/.*)?$
1st Capturing Group (.+)
.+ matches any character (except for line terminators)
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group (\d{4}-\d{2}-\d{2})
\d{4} matches a digit (equal to [0-9])
{4} Quantifier — Matches exactly 4 times
- matches the character - literally (case sensitive)
\d{2} matches a digit (equal to [0-9])
{2} Quantifier — Matches exactly 2 times
- matches the character - literally (case sensitive)
\d{2} matches a digit (equal to [0-9])
{2} Quantifier — Matches exactly 2 times
- matches the character - literally (case sensitive)
3rd Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
4th Capturing Group (.*)?
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string
In Ruby and Bash, you can use $ inside parentheses.
/(\S+?)/(\d{4}-\d{2}-\d{2})-(\d+)(/|$)
(This solution is similar to Pete Boughton's, but preserves the usage of $, which means end of line, rather than using \z, which means end of string.)