RegEx for capturing specific alphanumeric pattern - regex

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)

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

RegEx making a cost field validation

What could i use in a regexp variable to ensure a field contains only nummbers but also allows a full stop (period) and various money symbols (£,$)
Hope you can help!
Thanks
Here is what i have so far:
var validRegExp = /^[0-9]$/;
I would probably go with the following:
/^\d+(\.[\d]+){0,1}[€$]{0,1}$/gm
It matches at least one digit, then allows you to put zero or one period somewhere in there and then needs at least one digit after the period. At the end of it you may put one of the currency symbols explicitly named. You have to add all of the ones you want to support though.
Let try it for the following list:
3.50€
2$
.5
34.4.5
2$€
afasf
You will see that only the first two are matched correctly. Your final output are the ones in group 0.
const regex = /^\d+(\.[\d]+){0,1}[€$]{0,1}$/gm;
const str = `3.50€
2\$
.5
34.4.5
2\$€
afasf
`;
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}`);
});
}

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