Regex for capturing 4 consecutive numbers in an ID - regex

I need a formula to start at a a certain index and then get the next 4 consecutive numbers. Using REGEX. And testing whether they are in the range of 0000-4999.
^.{6}\d{4}$[0000-4999]
This is some code I have tried. Although I'm still new and don't understand regex.
The outcome needs to be as follows:
ID Number:
9202204720082
Get the following 4 numbers: 4720
Starting at index 7 (presuming indexes starts at 1)
So want to get numbers if indexes 7,8,9, and 10.
This is done to determine the gender in an ID.

May be this can help :
(?<=\d{6})\d{4}(?<=\d{3})
you can check it out in the regex101 link : Link to Regex

Your original expression is just fine, here we can slightly modify it to:
^.{6}(\d{4}).+$
which we are capturing our desired digits in this group (\d{4}).
DEMO
const regex = /^.{6}(\d{4}).+$/gm;
const str = `9202204720082`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
RegEx Circuit
jex.im visualizes regular expressions:

You don't need to use a regex for this. You could achieve the same result by simply getting a substring. Depending on your programming language you might do something like this:
JavaScript
var string = "9202204720082";
var index = 6;
console.log(string.substring(index, index + 4));
Ruby
string = '9202204720082'
index = 6
string[index, 4]
#=> "4720"
Perl
my $string = '9202204720082';
my $index = 6;
substr($string, $index, 4);
#=> "4720"

Related

Return only numbers from string using Google Analytics regex

I have a URL, lets say:
google.com/?ZipCode=77007
How can I return only the number part of the URL? I'm using google analytics regex.
I tried something like this:
\d{5}
and it matches the URL but doesn't isolate only the number.
Thanks!
If we wish to just get the zip code, these expressions might likely work:
ZipCode=([0-9]+)
ZipCode=([0-9]{5})
ZipCode=(\d+)
ZipCode=(\d{5})
which all have a missing capturing group (), that I'm guessing to be the issue here.
Demo 1
RegEx Circuit
jex.im visualizes regular expressions:
Demo
const regex = /ZipCode=(\d+)/gm;
const str = `google.com/?ZipCode=77007`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}

Regular expression with two unique requirements

I would like a regular expression that matches the following string:
"( one , two,three ,four, '')"
and extracts the following:
"one"
"two"
"three"
""
There could be any number of elements. The Regular expression:
"\[a-zA-Z\]+|(?<=')\\s*(?=')"
works, but the library I am using is not compatible with look-around assertions.
Do I have any options?
This expression would likely capture what we might want to extract here:
(\s+)?([A-Za-z]+)(\s+)?|'(.+)?'
which we might not want other additional boundaries and our desired outputs are in these two groups:
([A-Za-z]+)
(.+)
Demo
RegEx Circuit
jex.im visualizes regular expressions:
Test
const regex = /(\s+)?([A-Za-z]+)(\s+)?|'(.+)?'/gm;
const str = `"( one , two,three ,four, '')"`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}

RegEx for matching 3 alphabets and 1-2 digits

I am trying to write a regular expression to find a match in a text having at least 100 characters. The match should be like this - Any sub string within a string that contains at least 3 alphabet to begin with, at least 1 digit following it and a maximum of 2 digits following the 3 letters.
Examples -
abcjkhklfdpdn24hjkk - In this case I want to extract pdn24
hjdksfkpdf1lkjk - In this case I want to extract pdf1
hjgjdkspdg34kjfs dhj khk678jkfhlds1 - In this case I want both pdg34 and lds1
How do I write a regex for this ? The length of the starting letters for a match is always 3 and the digits length can be either 1 or 2 (not more not less)
This is what works if there are 2 digits after the 3 letter string.
[A-Za-z]{3}[0-9]{2}
But the length of the digits can vary between 1 and 2. How do I include the varying length in the regex?
The expression we wish to design is quite interesting. We can first add your original expression with a slight modification in a capturing group, then we should think of left and right boundaries around it. For instance, on the right we might want to use \D:
([A-Za-z]{3}[0-9]{1,2})\D
DEMO 1
We can surely define an exact restricted expression. However, this might just work.
Based on Cary Swoveland's advice, we can also use this expression, which is much better:
\p{L}{3}\d{1,2}(?!\d)
Test
re = /([A-Za-z]{3}[0-9]{1,2})\D/m
str = 'abcjkhklfdpdn24hjkk
hjdksfkpdf1lkjk
hjgjdkspdg34kjfs dhj khk678jkfhlds1 '
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
This script shows how the capturing group works:
const regex = /([A-Za-z]{3}[0-9]{1,2})\D/gm;
const str = `abcjkhklfdpdn24hjkk
hjdksfkpdf1lkjk
hjgjdkspdg34kjfs dhj khk678jkfhlds1 `;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
At least 3 alphabets: [a-zA-Z]{3,}
1 or 2 digits (not more not less): [0-9]{1,2}
This gives us:
/[a-zA-Z]{3,}[0-9]{1,2}/

RegEx for matching and excluding ' and "

I know that to negate a character like ' I can write [^'].
Bu I want to capture any character (repeated zero or more times) but this character should not be single or double quote:
"[^'""]*"
Is this the right syntax?
This expression might help you to do so:
([^"'])*
You might also want to use:
([^\x22\x27])*
Which you can simplify it as an expression maybe similar to so that to capture everything else that you wish except ' and " in a capturing group:
([^\x27\x22]*)
Graph
This graph shows how the expression would work and you can visualize other expressions in this link:
JavaScript Test
const regex = /([^\x27|\x22])*/gm;
const str = `anything else9*F&(A*&Fa09s7f'"'''"afa'"adfadsf`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}

Capturing empty instance in regex

Please pardon if my questions sounds basic. I have a text string with four values:
Field A|Field B|Field C|Field D
While getting an input one or more of these four values can be left blank, e.g:
Field A||Field C|Field D
Or
Field A||Field C||
I need to write a regex that can capture the values appropriately and assign it to specific buckets. Can someone please help?
Depending on the language you are using, they can be slightly different.
The implementation below is based on javascript. Essentially the pattern you're after is something like /(.*?)\|(.*?)\|(.*?)\|(.*)/
What this means is that you're capturing . everything and by specifying *? - this means non greedy capture until the first | pipe is seen.
Since we know there will be 4 groups and the last one will not have a | pipe, then by doing (.*) is adequate for the last set as it just means everything else on the string.
Try this:
const regex = /(.*?)\|(.*?)\|(.*?)\|(.*)/gm;
const str = `Field A||Field C|Field D`;
var m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}