JAVA Regex Explain [duplicate] - regex

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
[01]?\d{1,2}
can someone please explain the complete meaning of the above line.

This pattern describes a text that might (but not must) start with a 0 or 1 ([01]?) followed by one to two digits (\d{1,2}).

In short, this pattern used to test a number between 000 and 199. [01]? means a number starts with 0 or 1, and \d{1,2} means a one-digit or two-digit number.

Related

How do I simplify that regex? [duplicate]

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}

regular expression not start with zero and be in a specified limit [duplicate]

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}$

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.

Regular Expression : Finding the last 9 digits of a number and if the number is less than 9 digits than take the entire number [duplicate]

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.

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