RegEx making a cost field validation - regex

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

Related

How to detect the group of characters in a string using RegEx?

I have strings as follows :
M0122PD12XS1,
M1213234NW,
M1213234EFA1.
I need to read the last two/three characters in each string as follows. There will be at most one number after characters at the end regardless of numbers after it.
I need to read the last characters as follows :
M0122PD12XS1 => XS
M1213234NW => NW
M1213234EFA1=> EFA
I used the regex string as follows but it only read the last two/three characters when there are no other numbers next.
Regex string : ".{0,0}\D*$".
Any help is appreciated.
This might be what you need:
.*[0-9]([a-zA-Z]+)
look here for testing and here for visualization.
I think it should be
([A-Z]{2,})(?:[,.\s\d]+)?$
If no punctation required in the line ends, just
([A-Z]{2,})(?:[\d]+)?$
Where [A-Z]{2,} are 2 and more letters, [\d]+)? are optional numbers in the end of string.
Good question, we would be using the punctuation or space on the right side of our desired two or three letters with a simple expression:
[0-9]([A-Z]{2,3})([0-9])?[,.\s]
and on its left we would use the existing number as a left boundary.
Demo
const regex = /[0-9]([A-Z]{2,3})([0-9])?[,.\s]/gm;
const str = `M0122PD12XS1,
M1213234NW,
M1213234EFA1.
M0122PD12XS1
M1213234NW
M1213234EFA1
`;
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}`);
});
}
[A-Z]{2,3}((?=\d\b)|(?=\r)|(?=.)|(?=\b))
Finds 2 or 3 Alpha Characters [A-Z]{2,3}
Up to, but not including (?=) either
Single Digit, followed by a word boundary (?=\d\b)
Return Character (?=\r)
Period Character (?=\.)
Word Boundary (?=\b)

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

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