How do I simplify that regex? [duplicate] - regex

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
Closed 2 years ago.
I'm trying to get a remaining time data from a text but the times are written as months, weeks, days or hours rather then number. I've written this regex but it's a bit complicated. How can I simplify it?
[0-99] month[s]?|[0-99] week[s]?|[0-99] day[s]?|[0-99] hour[s]?
Example output:
2 days 4 hours

[0-99] is equivalent to a character set from 0 to 9, plus the character 9 - so it's equivalent to [0-9] - which is (often) equivalent to \d.
A character set with a single character in it is superfluous - just use the single character.
Finally, since the only thing that changes between the alternations is the word, put a group around the word and alternate inside the group:
\d (?:month|week|day|hour)s?\d
That's equivalent to your original pattern. But it sounds like you might be wanting to match up to 2 digits instead, in which case you can tweak it to:
\d{1,2} (?:month|week|day|hour)s?\d{1,2}

Related

RE2 Match from first character AFTER a character until FIRST space [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
How do I match everything after # until space?
(4 answers)
Regex everything after x until space
(1 answer)
Closed 28 days ago.
I really tried hard looking over the internet for an hour or so, trying to find if this question has already an answer somewhere, but no joy. If it already has an answer somewhere, feel free to link and close this one.
I am trying to match from AFTER a specific character until the FIRST space.
This is an example of the source string
blabla/1.2.3 [other stuff I dont care about]
I just want 1.2.3
I have tried so many different variants which I am not gonna pollute here all of them.
But the one I am most intrigued about is
\/.*\s
Apart from matching the / which I want to exclude, why does this match until the end of the line and not until the first space?
Other things I have tried
\/\b This just matches /
\/.*\b Matches almost everything until ]
\/.*\s? Again until end of line
\/.*(\s)? Ditto
\/.*\ Matches until the LAST whitespace excluding newline
And so on...

Regex.Matches problem in visual basic 2010 [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have this string
copiaElementos = "c'8 d'8 a8"
And when I do Regex.Matches(copiaElementos, "8.").Count() it returns 2
why is that? I don't understand, can anyone please give me a hand?
Thank you, best regards
That is because the . mathes one character. means you are matching an 8 followed by any charactrer, and there are exactly two of those (a space is considered a character too). Because the last one has no characters after it.
if you want to count the 8s in the string you should do Regex.Matches(copiaElementos, "8").Count(). Remember every character, even a space has its own meaning in regex.

Regex value of 0.0-5.0 [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 5 years ago.
I'm trying to create a regex string that allows values 0.0 - 5.0. I need the one decimal point to be required. The string below gets me there, but it also allows 5.1-5.9. How do I prevent 5.1-5.9 from being entered, and allow 5.0?
^[0-5]+(\.[0-9]{1})$
Try this regex:
^([0-4]\.[0-9]|5\.0)$
It matches any number from 0 to 4 then dot then any number.
it also matches 5.0
Note: Your regex has another problem that you used + after [0-5] which also matches 55 for example, so you need to remove the +. You also need to remove {1}, It won't make any change but it's useless.

Regular expression for 10 numeric digits [duplicate]

This question already has an answer here:
Java Regex for telephone number - Must Include only 8 digits with not more than 2 dash [duplicate]
(1 answer)
Closed 7 years ago.
10 numeric digits, may be in the following formats: 123-4-567890, 1234-567890 or 1234567890
What is the regular expression for above digits?
Any help is appreciated.
Thanks.
Assuming you mean any digit, 0-9, should be found if (and only if) it meets the three formats you presented, one regex that would work is
(([0-9]{3}-[0-9]{1}-[0-9]{6})|([0-9]{4}-[0-9]{6})|([0-9]{10}))
The above breaks down to three separate patterns, one for each case you presented, separated by regex's equivalent of "or", the | character. Each of the statements above contains [0-9], a character class which will match any digit. Following each character class is a {n} statement, which means "repeat the previous item n times".
Disclaimer, there is probably a cleverer way to do this with a shorter pattern, but my regex-foo isn't quite that advanced yet

Regex for range 1-1000 [duplicate]

This question already has answers here:
Regular expression where part of string must be number between 0-100
(7 answers)
Closed 1 year ago.
I need help creating a simple regex for a whole number range of 1-1000, with no special characters.
The two I have both seem to break or allow characters or not the full range:
^\d(\d)?(\d)?$
^[0-9]{1,3}$
Try this:
^([1-9][0-9]{0,2}|1000)$
[1-9][0-9]{0,2} matches any number between 1–999
1000 matches 1000
Use ^(.*[^0-9]|)(1000|[1-9]\d{0,2})([^0-9].*|)$ which will match 1000 or a non-zero digit followed by up to two further digits. It will also allow other characters on either end of the number.