Regular expression with two unique requirements - regex

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}`);
});
}

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}`);
});
}

RegEx for capturing specific alphanumeric pattern

I have this string and I want to know how extract the "10-K_20190304_29_1_20190515" part.
"nCABALLERO MARIA\r\n10.1-K\r\n10-K_20190304_29_1_20190515\r\n6204 DEPORTES SANTIAGO - PEÑALOLÉN"
I've tried this, .+(?<=_).+, but it brings me more characters that I need.
How do I solve this problem?
Here, we like to start with a simple left and right boundary and collect our desire data and save it in a capturing group ($1). Let's start with:
[0-9]{2}-.+[0-9]{8}
and lets add our capturing group:
([0-9]{2}-.+[0-9]{8})
DEMO
const regex = /[0-9]{2}-.+[0-9]{8}/gm;
const str = `nCABALLERO MARIA\\r\\n10.1-K\\r\\n10-K_20190304_29_1_20190515\\r\\n6204 DEPORTES SANTIAGO - PEÑALOLÉN`;
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
If this expression wasn't desired, it can be modified or changed in regex101.com.
RegEx Circuit
jex.im visualizes regular expressions:
If we wish to add more boundaries, we can certainly do so, depending on how our possible inputs might look like. For example, this expression has more boundaries:
([0-9]{2}-[A-Z]+_[0-9]{8}[0-9_]+.+?[0-9]{8})
DEMO
const regex = /([0-9]{2}-[A-Z]+_[0-9]{8}[0-9_]+.+?[0-9]{8})/gm;
const str = `nCABALLERO MARIA\\r\\n10.1-K\\r\\n10-K_20190304_29_1_20190515\\r\\n6204 DEPORTES SANTIAGO - PEÑALOLÉN`;
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}`);
});
}
You can also use split to extract the "10-K_20190304_29_1_20190515" part.
text.Split({“\r\n”},StringSplitOptions.None)(2)

Regex for capturing 4 consecutive numbers in an ID

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"

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}`);
});
}

Regular expression for match string with new line char

How use regular expression to match in text passphrase between Passphrase= string and \n char (Select: testpasssword)? The password can contain any characters.
My partial solution: Passphrase.*(?=\\nName) => Passphrase=testpasssword
[wifi_d0b5c2bc1d37_7078706c617967726f756e64_managed_psk]\nPassphrase=testpasssword\nName=pxplayground\nSSID=9079706c697967726f759e69\nFrequency=2462\nFavorite=true\nAutoConnect=true\nModified=2018-06-18T09:06:26.425176Z\nIPv4.method=dhcp\nIPv4.DHCP.LastAddress=0.0.0.0\nIPv6.method=auto\nIPv6.privacy=disabled\n
With QRegularExpression that supports PCRE regex syntax, you may use
QString str = "your_string";
QRegularExpression rx(R"(Passphrase=\K.+?(?=\\n))");
qDebug() << rx.match(str).captured(0);
See the regex demo
The R"(Passphrase=\K.+?(?=\\n))" is a raw string literal defining a Passphrase=\K.+?(?=\\n) regex pattern. It matches Passphrase= and then drops the matched text with the match reset operator \K and then matches 1 or more chars, as few as possible, up to the first \ char followed with n letter.
You may use a capturing group approach that looks simpler though:
QRegularExpression rx(R"(Passphrase=(.+?)\\n)");
qDebug() << rx.match(str).captured(1); // Here, grab Group 1 value!
See this regex demo.
The only thing you were missing is the the lazy quantifier telling your regex to only match as much as necessary and a positive lookbehind. The first one being a simple question mark after the plus, the second one just prefacing the phrase you want to match but not include by inputting ?<=. Check the code example to see it in action.
(?<=Passphrase=).+?(?=\\n)
const regex = /(?<=Passphrase=).+?(?=\\n)/gm;
const str = `[wifi_d0b5c2bc1d37_7078706c617967726f756e64_managed_psk]\\nPassphrase=testpasssword\\nName=pxplayground\\nSSID=9079706c697967726f759e69\\nFrequency=2462\\nFavorite=true\\nAutoConnect=true\\nModified=2018-06-18T09:06:26.425176Z\\nIPv4.method=dhcp\\nIPv4.DHCP.LastAddress=0.0.0.0\\nIPv6.method=auto\\nIPv6.privacy=disabled\\n
`;
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}`);
});
}