Find if either followed by non number or end of file - regex

I want to match the string b5 with optional $ in front of the b or tha 5 :
=b5
b$5
= $b$5
($b5)
But the 5 can't be followed by any number . And the b can't be preceded by any alphabet. So this should return false :
b55
ab5
I tried this :
\W\$*b\$*5\W
it works fine. i will match X=($b$5) but the problem is : it won't match anymore if the '5' is the last character in the line.
because 5 is last character

You can use
(?:\W|^)\$*b\$*5(?:\W|$)
(?:\W|^)\$*b\$*5\b
See the RE2 regex demo.
Details
(?:\W|^) - a non-capturing group matching either a non-word char or start of string
\$* - zero or more $ chars
b - a b char
\$* - zero or more $ chars
5 - a 5 char
(?:\W|$) - a non-capturing group matching either a non-word char or end of string or
\b - a word boundary.

Related

How can I use regex to match a string with several ending values including no end value?

I'm trying to come up with with a regex statement that will match and not match the following cases:
CT.test // (1) must match
CT.test (MONT) // (2) must match
CT.test (ABS) // (3) must match
CT.badsf // (4) must not match
CT.test (WOW) // (5) must not match
I've tried CT.test( \(MONT\)| \(ABS\)|^$) but that only matches cases 2 and 3 and not case 1.
What is a regex statement that will match case 1, 2 and 3 and not match cases 4 and 5?
You can use
^CT\.test(?: \((?:MONT|ABS)\))?$
See the regex demo
See the details here:
^ - start of string
CT\.test - (note the escaped dot): a CT.test string
(?: \((?:MONT|ABS)\))? - an optional sequence of
- a space
\( - a ( char
(?:MONT|ABS) - MONT or ABS string
\) - a ) char
$ - end of string.

Regex that does not accept sub strings of more than two 'b'

I need a regex that accepts all the strings consisting only of characters a and b, except those with more than two 'b' in a row.
For example, these should not match:
abb
ababbb
bba
bbbaa
bbb
bb
I came up with this, but it's not working
[a-b]+b{2,}[a-b]*
Here is my code:
int main() {
string input;
regex validator_regex("\b(?:b(?:a+b?)*|(?:a+b?)+)\b");
cout << "Hello, "<<endl;
while(regex_match(input,validator_regex)==false){
cout << "please enter your choice of regEx :"<<endl;
cin>>input;
if(regex_match(input,validator_regex)==false)
cout<<input+" is not a valid input"<<endl;
else
cout<<input+" is valid "<<endl;
}
}
Your pattern [a-b]+b{2,}[a-b]* matches 1 or more a or b chars until you match bb which is what you don't want. Also note that the string should be at least 3 characters long due to this part [a-b]+b{2,}
To not match 2 b chars in a row you can exclude those matches using a negative lookahead by matching optional chars a or b until you encounter bb
Note that [a-b] is the same as [ab]
\b(?![ab]*?bb)[ab]+\b
\b A word boundary
(?![ab]*?bb) Negative lookahead, assert not 0+ times a or b followed by bb to the right
[ab]+ Match 1+ occurrences of a or b
\b A word boundary
Regex demo
Without using lookarounds, you can match the strings that you don't want by matching a string that contains bb, and capture in group 1 the strings that you want to keep:
\b[ab]*bb[ab]*\b|\b([ab]+)\b
Regex demo
Or use an alternation matching either starting with b and optional repetitions of 1+ a chars followed by an optional b, or match 1+ repetitions of starting with a followed by an optional b
\b(?:b(?:a+b?)*|(?:a+b?)+)\b
Regex demo
The simplest regex is:
^(?!.*bb)[ab]+$
See live demo.
This regex works by adding a negative look ahead (anchored to start) for bb appearing anywhere within input consisting of a or b.
If zero length input should match, change [ab]+ to [ab]*.

Regex match strings with different values

for i,v in array
for i , v in array
for i , v in array
for i, v in array
for i,v in array
for i, v in array
for[\s+,.](.+)
https://regex101.com/r/Vd3w7C/2
How i could match anything after the v
but
i,v, and in array will have different values
i mean something like:
for ppp,gflgkf heekd gfvb
You could use
\bfor\s+[^\s,]+(?:\s*,\s*[^\s,]+)*\s+(.+)
The pattern matches:
\bfor\s+ Match for and 1+ whitespace chars
[^\s,]+ Match 1+ times any char except a whitspace char or ,
(?: Non capture group
\s*,\s*[^\s,]+ Match a comma between optional whitespace chars, and match at least a single char other than a comma or whitespace chars
)*\s+ Close the group and optionally repeat it followed by 1+ whitespace chars
(.+) Capture 1+ times any char except a newline in group 1
See a regex demo.

How to get lines until an empty newline

I want to get a bloc of lines which contains < or > operator until an empty newline
i try with this regex .*[<>][^,\r\n]+?\(.*\S.*,.*\S.*\).*(?:(\n).*)
You find here my example : https://regex101.com/r/UQYLB5/1/
Expected Result :
MATCH 1 :
BAR18>17M(3,5.2)V
MATCH 2 :
BAR19>1.243037M(3,5.2)V
INFORMATION PROCESS
TAKE B/F: 19V[1]
LIGHT PC CARD:
MATCH 3 :
TEFAL17>1.262259M(4.5,5.5)V
SISS17 : 1789-ID
LIGHT 19/17
MAPPING NICE :
MATCH 4 :
MASCARPONE19>493.818969M(3,5.2)V
BATA17 : CDER78945 -- 1875
LEFT ERREUR - CAME BACK
MATCH 5 :
REPAR_178>748.515487M(4.5,5.5)V
CHAN1 / STEREO MIX
If you don't want to match lines which could consist of spaces only, you could use match either < or > and match at least a non whitespace char \S in the following lines:
^[^<>\r\n]*[<>].*(?:\r?\n[^\r\n\S]*\S.*)*
The pattern will match:
^ Start of string
[^<>\r\n]* Match any char except < `
[<>].* Match either < or > and the rest of the line
(?: Non capture group
\r?\n Match a newline
[^\r\n\S]* Match any char except a newline
\S.* Match a non whitespace char and the rest of the line
)* Close the group and repeat 0+ times
Regex demo
If the first line should also contain a , after matching < or >:
^[^<>\r\n]*[<>][^\r\n,]*,.*(?:\r?\n[^\r\n\S]*\S.*)*
Regex demo

Using regexp to find a reoccurring pattern in MATLAB

input = ' 12Z taj 20501 jfdjda OCNL jtjajd ptpa 23Z jfdakdkf tjajdfk OCNL fdkadja 02Z fdjafsdk fkdsafk OCNL fdkafk dksakj = '
using regexp
regexp(input,'\s\d{2,4}Z\s.*(OCNL)','match')
I'm trying to get the output
[1,1] = 12Z taj 20501 jfdjda OCNL jtjajd ptpa
[1,2] = 23Z jfdakdkf tjajdfk OCNL fdkadja
[1,3] = 02Z fdjafsdk fkdsafk OCNL fdkafk dksakj
You may use
(?<!\S)\d{2,4}Z\s+.*?\S(?=\s\d{2,4}Z\s|\s*=\s*$)
See the regex demo.
Details
(?<!\S) - there must be a whitespace or start of string immediately to the left of the current location
\d{2,4} - 2, 3 or 4 digits
Z - a Z letter
\s+ - 1+ whitespaces
.*?\S - any zero or more chars as few as possible and then a non-whitespace
(?=\s\d{2,4}Z\s|\s*=\s*$) - there must be either of the two patterns immediately to the right of the current location:
\s\d{2,4}Z\s - a whitespace, 2, 3 or 4 digits, Z and a whitespace
| - or
\s*=\s*$ - a = enclosed with 0+ whitespace chars at the end of the string.