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)
Related
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}/
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}`);
});
}
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}`);
});
}
I am beginner in regular expression. For my case, I have an issue that alphabet and number might have space or might not have space like this.
4473 333hello 564 394844he hello
I need to take 333, 564
I have tried like this and they are not okay. How shall I do?
print(re.findall(r'\b\d{3}\b', "4473 333hello 564 394844he hello")) //it give ['564']
print(re.findall(r'\w+[0-9]{3}\w+', "4473 333hello 564 394844he hello")) // it give ['39484he']
Try this:
(?<!\d)\d{3}(?!\d)
Regex101
Details:
(?<!\d) - Negative Lookbehind to make sure there is no digit just before the match
\d{3} - Match 3 digits
(?!\d) - Negative lookahead to make sure there is no digit just after the match.
Try this
const regex = /\D\d{3}\s?/g;
const str = `4473 333hello 564 394844he hello
`;
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}`);
});
}
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}`);
});
}