pretty basic question, so I'll keep it short and sweet.
My current regex is \d* ( (\d){1,6} works, but is messy) - I want to grab all groups of numbers, i.e. 12345, 857.
How do I do it?
\d* matches any number of digits, including 0. Your string starts with 0 digits. Hey, a match!
Use \d+.
You are looking to do either \d+ or \d{1,} to match/capture your groups of digits.
Regular expression quantifiers are as followed:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
As per stated with grabbing your last group of digits in the following string(s):
google.com/185/586
google.com/389/754
Use a look ahead assertion: (?<=\d\/)(\d+), this will capture (586) and (754)
Related
In Scala, is it possible to actually insert commas via a regex to separate thousands in numbers where the comma definitely is not there to start with?
For example, I'd like to convert 30000.00 into 30,000.00.
I am not sure this is exactly what you need, but you can use this:
val formatter = java.text.NumberFormat.getNumberInstance
println(formatter.format(30000.00)) // prints 30,000
This is not scala based answer.
You can use regex \d{1,3}(?=(?:\d{3})+\.) to find the matches and substitute each match with the same match plus an extra comma $0,.
See the online demo.
Explanation:
\d{1,3} This matches a decimal character between 1 and 3 times
(?= Positive lookahead starts
(?: This indicates a Non-capturing group
\d{3} matches a digit exactly 3 times
) end of Non-capturing group.
+ matches the previous group one or more times
\. matches the character . literally
) Positive lookahead ends.
im looking for regex that can match string with requirement below.
must be 5 to 15 characters
Alphanumeric, can accept fully alphabet, if numeric are present, it must not exceed 5 digit and it can be in anywhere in the string.
Example accepted input
helloworld
123helloworld56
1h2e3l4l5oworld
12345
if the numeric digit exceeded 5 it shall be rejected. Example rejected input:
123456
123hello4567
So far i have tried while looking online and done some tweaking, but none work as expected.
^(?=.*\d?.*\d?.*\d?.*\d?.*\d?).{0,15}$
^(?=[a-zA-Z1-9]{5,15}$)[a-zA-Z]{1,15}[1-9]{0,5}$
^(?=.*\d){0,5}.{0,15}$
I have stuck on this for some time now, any help are appreciated!
If there can not be more than 5 digits in total, that means you should not be able to match 6 digits.
You can use a negative lookahead to assert what is on the right can not match 6 digits.
^(?!(?:[^\d\r\n]*\d){6})[a-zA-Z0-9]{5,15}$
Explanation
^ Start of string
(?! Negative lookahead, assert what is at the right is not
(?:[^\d\r\n]*\d){6} Match 6 times any char except a newline or a digit, then match a digit
) Close lookahead
[a-zA-Z0-9]{5,15} Match 5-15 times any of the listed in the character class
$ End of string
Regex demo
Note that using [1-9] in a character class does not match the 0, and \d will
About the patterns in the question
^(?=.*\d?.*\d?.*\d?.*\d?.*\d?).{0,15}$
Here, the lookahead will always be true as all the parts in it are optional. It could also match an empty string as the quantifier {0,15} starts at 0, which makes it optional.
^(?=[a-zA-Z1-9]{5,15}$)[a-zA-Z]{1,15}[1-9]{0,5}$
The pattern asserts a string with 5-15 times any of the listed in the character class. But the matching starts with 1-15 times a char a-zA-Z followed by matching 0-5 times a digit at the end of the string.
^(?=.*\d){0,5}.{0,15}$
The pattern optionally asserts 0-5 digits which is always true as it is optional. Then it matches 0-15 times any char.
I tried different regex which I found here but they are not working.
for example:
1111 = false
1112 = true
It's my homework so I must do it in regex :)
You can use this regex:
^(\d)(?!\1+$)\d{3}$
Explanation:
^ - Match line start
(\d) - match first digit and capture it in back reference #1 i.e. \1
(?!..) is a negative lookahead
(?!\1+$) means disallow the match if first digit is followed by same digit (captured group) till end.
\d{3}$ match next 3 digit followed by line end
How about this?
(?=^\d{4}$)(\d)+(?!\1)\d\d*
The first look-ahead group (?=^\d{4}$) insists that the whole string consists of 4 digits.
The first capture group then matches any number of digits: (\d)+.
After this, there must be a digit is different to the first capture group: (?!\1)\d
Finally, there can be any number of digits trailing: \d*
what is the best way to build a regex for numbers between 10 and 240, and another one between 10 and 360?
Regexes are no good to deal with numbers. Unless this is the only alternative that you got, you should probably choose another solution.
10-240: ^(?:2(?:[0-3]\d|40)|1\d\d|[1-9]\d)$
Explanation:
^: Anchor that match the beginning of the string
(?: Non-capturing group (more performant than capturing groups). I use those for alternation.
2: Literal character '2'
[0-3]: A single digit between 0 and 3.
\d: A single digit character (0-9)
|: Or
3-6. 2(?:[0-3]\d|40): A number that starts with 2 followed by 0-3 and any digit or literally '40'. That match 200-240
|1\d\d: Or one followed by two digits (0-9). That match 100-199.
|[1-9]\d : Or a digit between 1-9 followed by any digit (0-9). That match 10-99.
$: Anchor that match the end of the string.
Test it here: https://regex101.com/r/rO4fZ0/1
10-360: ^(?:3(?:[0-5]\d|60)|[12]\d\d|[1-9]\d)$
3(?:[0-5]\d|60): Literal character 3 followed by 0-5 and any digit or literally 60. That match 300-360.
|[12]\d\d: Or one or two followed by two digits (0-9). That match 100-299.
|[1-9]\d : Or a digit between 1-9 followed by any digit (0-9). That match 10-99.
Test it here: https://regex101.com/r/lD8oM4/1
The best way to do so, is with a tester, http://regexr.com
Here is the RegEx for the 10 to 240 match.
^(([1-9][0-9])|(1[0-9][0-9])|(2[0-3][0-9])|(240))$
However, I do feel this is probably not the right tool of what you want to achieve.
Mike
Why is the below pattern match is successful? Am I missing something?
$a="pattern";
if($a =~ /[0-9]*/){
print "Contains\n";
}
The * quantifier matches 0 or more. And the pattern does match exactly zero digits.
You might want to use + which would denote a match 1 or more times.
Quoting from perldoc perlre:
Quantifiers
The following standard quantifiers are recognized:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
Using * as a quantifier means zero or more instances. In this case it is matching with zero at the position just before the p of the target string.
To match at least one digit use + quantifier instead.