This question already has answers here:
Regex exactly n OR m times
(6 answers)
Closed 8 years ago.
When I want to test if input string is at least length 10 and at most length 13, I use this pattern: [0-9]{10,13}
But how would you test if the string length is 10 or 13 and nothing in between?
EDIT: I see two dominant solutions:
1) Using length1 OR length2
2) Using length1 + optional characters of (length2 - length1)
Just wondering: are there any performance differences between the two?
Another option would be
^[0-9]{10}(?:[0-9]{3})?$
regex101 demo
use this expression ^((?:\d{3}){3,4}\d)$
Related
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}
This question already has answers here:
Regular expression for 7 digits not starting with 0 or 1
(2 answers)
Closed 2 years ago.
I am trying to write a Regular expression that will not start with zero and be in a specified limit, say upto 7 digit long
Valid Example:
100,
2345678,
Invalid Example:
01,
0256,
12345678
I tried the following
pattern:
^[1-9][0-9]{1,7}$
But this not help
Try this, this should work: ^[1-9][0-9]{0,6}$
try to below way it's working
^[1-9][0-9]{0,6}$
This question already has answers here:
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 4 years ago.
I have been working on an regular expression which say consists of more than 9 digits ( 12345678910111213 )
With the help of regex \d{9}(?!\d) i am able to find the last 9 digits of the number.
But when the entire number is less than 9 digits how do i take the entire number as pass it. is there any such regular expression.
\d{1,9}(?!\d) will take as many as possible from the end, up to 9.
This question already has answers here:
How to find the length of a string in R
(6 answers)
Closed 8 years ago.
In R, I need to remove string that exceeds the length of 7 characters, from a column in a data frame.
My code is,
memos.to <- as.data.frame(apply(memos.to,2,function(x)gsub('/^[a-zA-Z0-9]{7,}$/', NA ,x)))
and it doesn't seem to work. What's wrong here?
The easiest way is to just check the string length.
Don't know R lang, but all things being equal, if it conforms to the minimal modern regex's
One of these should match as far as regex is concerned
/.{8,}/ using Dot-all modifier as external flag
or
/(?s).{8,}/
or
/[\S\s]{8,}/ if Dot-all not available
if you are only considering [a-zA-Z0-9] chars
/^[a-zA-Z0-9]{8,}$/
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.