regex + capturing groups with varying conditions - regex

working on the regex here https://regex101.com/r/wI2cG1/1
this is the data:
K'1234567
K'123456789
K'123456
I am interested in the digits after K'
I am looking to do this using regex but not sure if it can be done. What I want is:
if the number has 6 digits return the first 2 digits e.g. 12
if the number has 7 digits return the first 3 digits e.g. 123
if the number has 9 digits return the first 4 digits e.g. 1234
also
if the number has 10 or 11 digits return the first 3 digits e.g. 123
and I want to return these to different capturing group names or if possible the same capturing group name.

It's possible to maintain the results in one group using the branch reset feature:
K'(?|(\d{2,3})\d{4}|(\d{4})\d{5}|(\d{3})\d{7,8})\b
Regex Demo

Related

Google sheets to regexextract 4 digits only. Do not extract from longer number, say 1234 from 123456 [duplicate]

This question already has answers here:
regular expression to match exactly 5 digits
(5 answers)
Closed 27 days ago.
I have data where users enter 3 types of data:
random
4 digit / 6 digit
6 digit / 4 digit
I was looking for REGEXEXTRACT code in google sheets that will pick only 4 digit group. It will not be tricked by longer number, say "Article 1234124 Store 4444" it should return 4444 not 1234.
1234/1234567 --> 1234
1234567/5555 --> 5555
Article 1234124 Store 4444 -->4444
this would be a sample code, but google sheets does not support lookahead or lookbehind,
not a digit, 4 digits, not a digit:
=REGEXEXTRACT(A2,"(?<!\d)\d{4}(?!\d)")
thanks
You could use the following regex pattern:
(?:^|\D)(\d{4})(?:\D|$)
This pattern says to match:
(?:^|\D) Either a non digit or the start of the string
(\d{4}) A 4 digit number
(?:\D|$) Either a non digit or the end of the string
This ensures that we only match 4 digit numbers.
Sample code:
=REGEXEXTRACT(A2,"(?:^|\D)(\d{4})(?:\D|$)")

Regex match 2 letters and up to 10 digits afterwards

I have the following regex which is matching the first 2 letters RR and then 4 numbers after.
RR[0-9]{4}
How can I change it to detect the first 2 letters RR and then up to 10 digits afterwards?
I know I can do...
^[0-9]*$
To match all numbers but how can I limit this and add it to the first regex?
You can use RR\d{0,10}. This matches RR, followed by 0 to 10 digits, i.e. up to 10 digits.

C++ multiple regex conditions syntax

In other words, is there an AND operator in c++ regex? Normally I would just use | but it doesn't work
For example I want to return only 2 and 1 digit numbers
string subject("This 91 - 500abc7 is a 5 test");
regex re("\\d\\d");
This only returns 2 digit numbers, how do I add a second condition to also match single digits "\d"
Result should be:
91 - 7 - 5
It is not a "and" you want to have 1 OR 2 digits (\d|\d\d).
but regex have notation for numbered repetition: \d{1,2}
Issue is that \d{1,2} would match 50 in 500.
So you might add (negative) look ahead/behind:
(?<!\d)\d{1,2}(?!\d) (1 or 2 digits not preceded and followed by another digit)
so std::regex re(R"((?<!\d)\d{1,2}(?!\d))");

regex expression in javascript

I am trying to build a regex in javascript to match a 9-digit number with these characteristics:
First 3 digits should not be ‘0’ ,
4th and 5th Digit should not be ‘0’,
Last 4 digits should not be ‘0000’,
First 3 digits should not be ‘666’,
Last 3 Digits Should not be greater than ‘899’
Can someone please help me out with this.
Here is my current regex:
/^666[^0]{3}[1-9]{2}0000$/
, but it’s not meeting the criteria
Try This regular expression it will work ^(?!(000)|(666))[0-9]{3}[1-9]{2}[0-9][0-8][0-9]{2}(?<!0000)$. See demo here
Try this ^[^06]{3}[^0]{2}[^0][1-8][^0]{2}$
Below is my explanation for it
First 3 digits should not be ‘0’
First 3 digits should not be ‘666’,-> ^[^06]{3}
4th and 5th Digit should not be ‘0’, -> [^0]{2}
Last 4 digits should not be ‘0000’, -> [^0]{4}
Last 3 Digits Should not be greater than ‘899’ -> [1-8][^0]{2}$
Because all nine characters need to be a digit, you might use lookahead from the beginning to check that there are 9 digits followed by the end of the string, which will make the subsequent groups easier to manage. Then, you need to utilize character sets and negative lookahead. The first two conditions look to collapse together - the first five characters need to be other than 0:
/^(?=\d{9}$)(?!666)[^0]{5}(?!0000)\d[^9]/
const re = /^(?=\d{9}$)(?!666)[^0]{5}(?!0000).[^9]/;
`555555555
5555555555
055555555
555505555
666555555
555550000
555550001
555550900
555550953`
.split('\n')
.forEach(n => console.log(re.test(String(n))));
Explanation:
555555555 true
5555555555 false; 10 digits, not 9
055555555 false: has 0 in first 5 digits
555505555 false: has 0 in first 5 digits
666555555 false: starts with 666
555550000 false: ends with 0000
555550001 true
555550900 false: sixth digit is a 9 (so last 3 digits are 9xx, which is greater than 899)
555550953 false: same as above
https://regex101.com/r/Vpwbk0/1

regex: Numbers and spaces (10 or 14 numbers)

How I can write a regex which accepts 10 or 14 digits separated by a single space in groups of 1,2 or 3 digits?
examples:
123 45 6 789 1 is valid
1234 567 8 9 1 is not valid (group of 4 digits)
123 45 6 789 109 123 8374 is not valid (not 10 or 14 digits)
EDIT
This is what I have tried so far
[0-9 ]{10,14}+
But it validates also 11,12,13 numbers, and doesn't check for group of numbers
You may use this regex with lookahead assertion:
^(?=(?:\d ?){10}(?:(?:\d ?){4})?$)\d{1,3}(?: \d{1,3})+$
RegEx Demo
Here (?=...) is lookahead assertion that enforces presence of 10 or 14 digits in input.
\d{1,3}(?: \d{1,3})+ matches input with 1 to 3 digits separated by space with no space allowed at start or end.
aggtr,
You can match your use case with the following:
^(?:\d\s?){10}$|^(?:\d\s?){14}$
^ means the beginning of the string and $ means the end of the string.
(?:...) means a non-capturing group. Thus, the part before the | means a string that starts and has a non-capturing group of a decimal followed by an optional space that has exactly 10 items followed by the end of the string. By putting the | you allow for either 10 or 14 of your pattern.
Edit I missed the part of your requirement to have the digits grouped by 1, 2, or 3 digits.