RegEx for matching and excluding ' and " - regex

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

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 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)

Regx to find the string staring with word and ending either with ?/end of line but not containing a specific word

Regx to find the string staring with word and ending either with ?/end of line but not containing a specific word
For e.g., I have following URL with different format and want to capture specific part (Page Identifier )
Home: https://www.example.com/course/home#/
courseSummary: https://www.example.com/tag/mypage/course/#/courseSummary?courseName=abc&courceTitle=MyTitle
grounddetails : https://www.example.com/tag/mypage/course/#/options/grounddetails
Certification : https://www.example.com/tag/mypage/course/#/options/Certification/segment
customer: https://www.example.com/tag/mypage/course/#/checkout/customer
But whenever the 'confirmation' word contain in URL then it SHOULD NOT match.
https://www.example.com/tag/mypage/course/#/**confirmation**?success=true
Could you please help to compose the regex for it
You may try this:
^\w+ *: *http(?:s):\/\/(?!.*confirmation).*(?:\?|\n)$
Regex 101 Demo
const regex = /^\w+ *: *http(?:s):\/\/(?!.*confirmation).*(?:\?|\n)$/gm;
const str = `Home: https://www.example.com/course/home#/
courseSummary: https://www.example.com/tag/mypage/course/#/courseSummary?courseName=abc&courceTitle=MyTitle
grounddetails : https://www.example.com/tag/mypage/course/#/options/grounddetails
Certification : https://www.example.com/tag/mypage/course/#/options/Certification/segment
customer: https://www.example.com/tag/mypage/course/#/checkout/customer
But whenever the 'confirmation' word contain in URL then it SHOULD NOT match.
blalba: https://www.example.com/tag/mypage/course/#/**confirmat**?success=true
blalba: https://www.example.com/tag/mypage/course/#/**confirmation**?success=true
blalba: https://www.example.com/tag/mypage/course/#/**confirmatio**?success=true
`;
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}`);
});
}