Regular expression for boolean expression almost working - regex

My regular expression is:
(?:^ *)?(?:\\(*|())[0-9](?: +(?:AND|OR) +(?:\\(|[0-9]))?(?: *\\)|\\1)
And I am trying to use this as a test string:
1 AND 2 OR (3 AND 4 OR (2 AND 1))
If I replace all matches it finds, I end up with 1 OR (1 OR 1) when the final string should just be 1 (replacing each match with 1).
I think it is the +(?:\(|[0-9])) part. The regex seems to disregard instances of number JOIN (number
I pulled this regex from the second answer on this question
And the comments say it is supposed to find situations of 3 AND (1 etc... but it is not when I use it.
Does anyone know how I might modify this regular expression to properly group a boolean expression?

Related

RegEx - Match a number that has different beginnings

I'm new to RegEx and I'm trying to match a specific number that has 8 digits, and has 3 start options:
00
15620450000
VS
For Example:
1562045000012345678
VS12345678
0012345678
12345678
I don't want to match the 4th option.
Right now I have managed to match the first and third options, but I'm having problems with the second one, I wrote this expression, trying to match the 8 digits under 'Project':
156204500|VS|00(?<Project>\d{8})
What should I do?
Thanks
With your shown samples, please try following regex once.
^(?:00|15620450000|VS)(\d{8})$
OR to match it with Project try:
^(?:00|15620450000|VS)(?<Project>\d{8})$
Online demo for above regex
Explanation: Adding detailed explanation for above.
^(?:00|15620450000|VS) ##Checking value from starting and in a non-capturing group matching 00/15620450000/VS here as per question.
(?<Project>\d{8} ##Creating group named Project which is making sure value has only 8 digits till end of value.
)$ ##Closing capturing group here.
Let's understand why your solution failed that will help you get around such kind of problems in the future. Your regex, 156204500|VS|00(\d{8}) is processed as follows:
156204500 OR VS OR 00(\d{8})
In arithmetic,
1 + 2 + 3 (4 + 5) <--- (4 + 5) is multiplied with only 3
is different from
(1 + 2 + 3) (4 + 5) <--- (4 + 5) is multiplied with (1 + 2 + 3)
This rule is applicable to RegEx as well. Obviously, you intended to use the second form.
By now, you must have already figured out the following solution:
(15620450000|VS|00)(\d{8})
Note that unless you want to capture a group, a capturing group does not make sense and this is where regex has another concept called non-capturing group which you obtain by putting ?: as the first thing in the parentheses. With a non-capturing group, the final solution becomes:
(?:15620450000|VS|00)\d{8}

Regular Expression Match Groups [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 2 years ago.
I have the following regular expression:
(.*(\d*)(-)(\d*).*)
It correctly matches the following string:
Court 19-24
However, the second group is empty - Group 1: Court 19-24, Group 2: [empty], Group 3: -, Group 4: 24
What is wrong with my regular expression that the second group doesn't contain 19?
Maybe you are looking for something like this?
let re = /.* (\d+)(-)(\d+).*/;
let str = 'Court 19-24';
var match = re.exec(str);
console.log('group 0:', match[0])
console.log('group 1:', match[1])
console.log('group 2:', match[2])
console.log('group 3:', match[3])
Group indexes annotated
(.(\d)(-)(\d*).*)
1 2 3 4
Not sure what you expected here?
To match 19 and 24 into two separate groups, use something like this:
(\d+)-(\d+)
To also match the court name into a group, a non-greedy star works.
(.*?) (\d+)-(\d+)
I bet your initial regex was more like this:
(.*(\d*)(-)(\d*).*)
This will behave closer to what you describe, because
. also matches digits, i.e. .* will go up to the -, and
* is not required to match anything at all, i.e. \d* it will happily match zero digits, causing group 2 to be empty.
Lessons:
Don't use * when you expect at least something to be there. Prefer + in this case.
Be wary of the greedy star, especially when used with the unspecific . it can match things you did not intend.
You don't have to put parenthesis around everything in regular expressions. Only add groups around things you want to extract (i.e. "capture"), or around things you want to match/fail/repeat as one (i.e. "make atomic").

How to use the interval quantifier from 0 up to a specific number Regular expressions in Google Sheets?

I'm trying to solve this issue:
How to use the interval quantifier curlybraces "{}" from 0 up to 4 in a REGEXMATCH Google Sheets formula to make it match only the occurrences from zero occurrence up to 4 occurrences and no more?
Here the source and context I started first from (section Quantifiers — * + ? and {}) Regex tutorial — A quick cheatsheet by examples
Specifying the following:
a(bc){2,5} matches a string that has a followed by 2 up to 5 copies of the sequence bc
My formula is:
=REGEXMATCH($A7,"a(bc){0,4}")
Here the 1st input in A7:
abcbcbcbcbc
Contrary to expectation, it returns TRUE despite A7 having more than 4 bc's as input in A7.
The same contrary to expectation result occurs for the following intervals {1,4} and {2,4} in :
=REGEXMATCH($A7,"a(bc){1,4}")
=REGEXMATCH($A7,"a(bc){2,4}")
It still returns matches despite 5 occurences of bc's sequences in those latter two cases as well.
Here the Sheet:
quantifier interval in regex from zero to defined interval end
I read the general regex info answer here Learning Regular Expressions [closed] but couldn't find the solution.
How to make it return FALSE for any input of more than 4 bc's in A7?
Thanks a lot for your help!
A regex does not have to match the entire string you are checking it against by default. The function will return True if the regex matches any substring of the provided string.
To change that behaviour add the character ^ to match the start of the subject string and the character $ to match its end.
For example: =REGEXMATCH($A7,"^a(bc){0,4}$") will not match abcbcbcbcbc.

Need matching opening and closed brackets in regular expression with dynamic value in between

We have a requirement to enter values like this in a textbox:
((1 OR 2) AND (3)) or (1 AND (2 or 3 AND 4))
The numbers 1,2 and `3 are dynamic in nature based on the number of rows in a grid.
There are three validations that we want to accomplish.
restrict the numbers entered which will be based on the number of rows in a grid;
should only allow AND and OR text;
parenthesis should be well formed;
Preferably we need the regular expression in JavaScript so that it can be changed during run time.
this pattern (\((?:([1-4]|\s|and|or)|(?1))+\))|(?2) when replaced with nothing should eat up every thing if your text box is valid and leave you with something if it is not.
Demo
Desclaimer:
it will not validate something like ((1 OR 2) AND (3)) or (1 AND (2 or 3 AND )) where the last AND is not followed by an argument, it only checks for balanced brackets.

Regular Expression for US and other format zip [duplicate]

This question already has answers here:
What is the ultimate postal code and zip regex?
(20 answers)
Closed 9 years ago.
Am working on regular expression for following formats of zip in groovy
Includes a letter (L12345)
Includes a dash plus 4 more numbers (77056-1234)
Includes spaces (77056 1234)
I have this "^\d{5}(-\d{4})?\$" but its not matching the required formats. Could anyone please help me?
^\d{5}(?:[-\s]\d{4})?$
^ = Start of the string.
\d{5} = Match 5 digits (for condition 1, 2, 3)
(?:…) = Grouping
[-\s] = Match a space (for condition 3) or a hyphen (for condition 2)
\d{4} = Match 4 digits (for condition 2, 3)
…? = The pattern before it is optional (for condition 1)
$ = End of the string.
This is from the following question, hope it helps
regex for zip-code
For the optiona startingil letter use
[A-Z]?
to make the letter optional. {1} is redundant. (Of course you could also write [A-Z]{0,1} which would mean the same, but that's what the ? is there for.)
I think it should go after the ^ but haven't had a chance to test