This question already has answers here:
Only add thousand separator before decimal comma
(3 answers)
Closed 3 years ago.
This is a quiz exercise
Use substitution to put commas in all numbers to separate the thousands. ie: 12345678 → 12,345,678. The number could be in a sentence, and there may be more than one number in the sentence.
I tried the code
/(\d{3})(\d{3})/g
Results returns
Test 6/19: 100013541615681651 should become 100,013,541,615,681,651. Did you forget the global flag?
Tried it
/\d(?=(?:\d{3})+(?!\d))/g
Results returns
Test 14/19: Your regex is incorrectly replacing the number in the following string: Your ticket number is A87654
Regex demo
Matched String
12345678901234567890
12345678
1234567
Unmatched String
A87654
Expected results
123,456789,012345,67890
123,45678
123,4567
A87654
Reference
Matching Whole Lines of Text
Lookahead for a repeated group of 3 digits, followed eventually by negative lookahead for a digit:
\d(?=(?:\d{3})+(?!\d))
and replace with
$0,
https://regex101.com/r/QAHt6E/5
Here's a regex that won't match numbers which are connected to some letters as well:
(?=\b\d|\G)\d+?(?=(?:\d{3})+\b)
(?=\b\d|\G) is a lookahead for either a word break and a digit, or the end of the previous match . \d+? looks for a minimal number of digits and then (?=(?:\d{3})+\b) asserts that it is followed at least one group of 3 digits and then another word break. The word breaks at the beginning and end of the regex mean that it cannot match numbers that are part of an alphanumeric string.
This should be replaced with $0, to insert commas into the numbers.
Demo on regex101
Related
I'm trying to create regex to retrieve last number if there was a number or any number if there wasn't any from a string.
Examples:
6 łyżek stopionego masła -> 6
5 łyżek blabla, 6 łyżek masła -> 6
5 łyżek mąki lub masła -> 5
I'm matching only on masła (changing variable) so it has to be included in regex
EDIT:
I cannot explain what I actually need:
Here is regex101 example: https://regex101.com/r/pEeRk3/1
EDIT2:
Emma's solution works great, but I would need to parse decimals and 2multiple digit numbers as well, meaning that those would match as well:
https://regex101.com/r/pEeRk3/3 - I added examples with answers in the link
If you want to match the last occurence of a digit with a decimal and you word has to follow this value, you might use lookarounds:
(?<!\S)\d+(?:\.\d+)?(?!\S)(?!.*\d)(?=.*masła)
(?<!\S)\d+(?:\.\d+)?(?!\S) Match 1+ digits with an optional past to match a dot and 1+ digits
(?!.*\d) assert that there are no more digits following
(?=.*masła) Assert what is on the right is your word
Regex demo
Or you might use a capturing group:
(?<!\S)(\d+(?:\.\d+)?)[^\d\n]* masła(?!\S)[^\d\n]*$
Regex demo
This expression might simply suffice:
.*([0-9])
if we are interested in one digit only, or
.*([0-9]+)
if multiple digits might be desired.
Demo 1
If those strings with masła are desired, we can expand our expression to:
(?=.*masła).*([0-9])
Demo 2
If we would not be validating our numbers and our number would be valid, with commas or dots, then this expression might likely return our desired output:
(?=.*masła)([0-9,.]+)(\D*)$
Demo 3
This question already has answers here:
Regex to match exactly n occurrences of letters and m occurrences of digits
(3 answers)
Closed 4 years ago.
I am looking for a regex that matches the following:
2 times the character 'a' and 3 times the character 'b'.
Additionally, the characters do not have to be subsequent, meaning that not only 'aabbb' and 'bbaaa' should be allowed, but also 'ababb', 'abbab' and so forth.
By the sound of it this should be an easy task, but atm I just can't wrap my head around it. Redirection to a good read is appreciated.
You need to use positive lookaheads. This is the same as the password validation problem described here.
Edit:
A positive lookahed will allow you to check a pattern against the string without changing where the next part of the regex matches. This means that you can test multiple regex patterns at the current position of the string and for the regex to match all the positive lookaheads will have to match.
In your case you are looking for 2 a' and 3 b's so the regex to match exactly 2 a's anywhere in the string is /^[^a]*a[^a]*a[^a]*$/ and for 3 b's is /^[^b]*b[^b]*b[^b]*b[^b]*$/ we now need to combine these so that we can match both together as follows /^(?=[^a]*a[^a]*a[^a]*$)(?=[^b]*b[^b]*b[^b]*b[^b]*$).*$/. This will start at the beginning of the string with the ^ anchor, then look for exactly 2 a's then the end of the string. Then because that was a positive lookahead the (?= ... ) the position for the next part of the pattern to match at in the string wont move so we are still at the start of the string and now match exactly 3 b's. As this is a positive lookahead we are still at the beginning of the string but now know that we have 2 a's and 3'b in the string so we match the whole of the string with .*$.
This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 4 years ago.
I would like to match 1 or more capital letters, [A-Z]+ followed by 0 or more numbers, [0-9]* but the entire string needs to be less than or equal to 8 characters in total.
No matter what regex I come up with the total length seems to be ignored. Here is what I've tried.
^[A-Z]+[0-9]*{1,8}$ //Range ignored, will not work on regex101.com but will on rubular.com/
^([A-Z]+[0-9]*){1,8}$ //Range ignored
^(([A-Z]+[0-9]*){1,8})$ //Range ignored
Is this not possible in regex? Do I just need to do the range check in the language I'm writing in? That's fine but I thought it would be cleaner to keep in all in regex syntax. Thanks
The behaviour is expected. When you write the following pattern:
^([A-Z]+[0-9]*){1,8}$
The {1,8} quantifier is telling the regex to repeat the previous pattern, therefore the capturing group in this case, between one to eight times. Due to the greedyness of your operators, you will match and capture indefinitely.
You need to use a lookahead to obtain the desired behaviour:
^(?=.{1,8}$)[A-Z]+[0-9]*$
^ Assert beginning of string.
(?=.{1,8}$) Ensure that the string that follows is between one and eight characters in length.
[A-Z]+[0-9]*$ Match any upper case letters, one or more, and any digits, zero or more.
$ Asserts position end of string.
See working demo here.
The regex ^([A-Z]+[0-9]*){1,8}$ would match [A-Z]+[0-9]* 1 - 8 times. That would match for example a repetition of 8 times A1A1A1A1A1A1A1A1 but not a repetition of 9 times A1A1A1A1A1A1A1A1A1
You might use a positive lookahead (?=[A-Z0-9]{1,8}$) to assert the length of the string:
^(?=[A-Z0-9]{1,8}$)[A-Z]+[0-9]*$
That would match
^ From the start of the string
(?=[A-Z0-9]{1,8}$) Positive lookahead to assert that what follows matches any of the characters in the character class [A-Z0-9] 1 - 8 times and assert the end of the string.
[A-Z]+[0-9]*$ Match one or more times an uppercase character followed by zero or more times a digit and assert the end of the string. $
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
I need to validate string input with regex, rules are:
String should not be number less than 2 and not bigger than 9999 (2-9999)
String should not have zeros before number (ex: no 0002, 0022, 0222)
I really need to accomplish this by regex so any other solution is not acceptable.
Try this:
/^[2-9]|[1-9][0-9]{1,3}$/
To implement your first condition:
String should not be number less than 2 and not bigger than 9999 (2-9999)
There is two cases:
Single digits : [2-9] This is a single character in the range between 2 and 9.
Multiple digits: [1-9][0-9]{1,3} This is a two-three-four-digit number which all digits are in the range 1 and 9.
Note1: {1,3} limits second character class to just accept one or two or three digits.
Note2: ^ means start of string and $ means end of string.
By the way, your second condition isn't defined in pattern above at all. (I mean it doesn't match any number which stars with 0, So all fine.)
Try this
^(?!0|1$)\d{1,4}$
Regex demo
Explanation:
^\d{1,4}$: matches 0-9999
(?!0)...: not have zeros before number (ex: no 0002, 0022, 0222)
(?!1$)...: not be number less than 2 (==1)
(?!…): Negative lookahead sample
\d: One digit from 0 to 9 sample
^: Start of string or start of line depending on multiline mode
$: End of string or end of line depending on multiline mode
My RegExp is very rusty! I have two questions, related to the following RegExp
Question Part 1
I'm trying to get the following RegExp to work
^.*\d{1}\.{1}\d{1}[A-Z]{5}.*$
What I'm trying to pass is x1.1SMITHx or x1.1.JONESx
Where x can be anything of any length but the SMITH or JONES part of the input string is checked for 5 upper case characters only
So:
some preamble 1.1SMITH some more characters 123
xyz1.1JONES some more characters 123
both pass
But
another bit of string1.1SMITHABC some more characters 123
xyz1.1ME some more characters 123
Should not pass because SMITH now contains 3 additional characters, ABC, and ME is only 2 characters.
I only pass if after 1.1 there are 5 characters only
Question Part 2
How do I match on specific number of digits ?
Not bothered what they are, it's the number of them that I can't get working
if I use ^\d{1}$ I'd have thought it'll only pass if one digit is present
It will pass 5 but it also passes 67
It should fail 67 as it's two digits in length.
The RegExp should pass only if 1 digit is present.
For the first one, check out this regex:
^.*\d\.\d[A-Z]{5}[^A-Z]*$
Before solving the problem, I made it easier to read by removing all of the {1}. This is an unnecessary qualifier since regex will default to looking for one character (/abc/ matches abc not aaabbbccc).
To fix the issue, we just need to replace your final .*. This says match 0+ characters of anything. If we make this "dot-match-all" more specific (i.e. [^A-Z]), you won't match SMITHABC.
I came up with a number of solution but I like these most. If your RegEx engine supports negative look-ahead and negative look-behind, you can use this:
Part 1: (?<![A-Z])[A-Z]{5}(?![A-Z])
Part 2: (?<!\d)\d(?!\d)
Both have a pattern of (?<!expr)expr(?!expr).
(?<!...) is a negative look-behind, meaning the match isn't preceded by the expression in the bracket.
(?!...) is a negative look-ahead, meaning the match isn't followed by the expression in the bracket.
So: for the first pattern, it means "find 5 uppercase characters that are neither preceded nor followed by another uppercase character". In other words, match exactly 5 uppercase characters.
The second pattern works the same way: find a digit that is not preceded or followed by another digit.
You can try it on Regex 101.