Regex for logic criteria [duplicate] - regex

This question already has answers here:
Validate a Boolean expression with brackets in C#
(6 answers)
Closed 8 years ago.
I have a input text box to enter a logic criteria.
Following are the possible inputs:
1 OR 2
1 AND 2
(1 OR 2) AND 3
(1 OR 2) OR 3
(1 AND 2) AND 3
(1 AND 2) OR 3
1 AND (2 OR 3)
1 OR (2 OR 3)
1 AND (2 AND 3)
1 OR (2 AND 3)
(1 OR 2) AND (3 OR 4)
(1 OR 2) OR (3 OR 4)
(1 AND 2) OR (3 AND 4)
(1 OR 2) AND (3 OR 4) AND (5 OR 6)
Can anyone provide a RegEx to validate such input?

Assuming you can nest parentheses, you cannot do this with straight regular expressions, because you cannot validate arbitrarily nested parentheses with a regular expression.
The more typical way to validate this input is to break the process into two steps. Use a family of regular expressions to tokenize the input, and then use a simple grammar to validate the resulting sequence of tokens. An LALR(1) grammar such as what yacc supports makes this problem trivial.
I think there are some extended regex forms that add the necessary functionality that you could match arbitrarily nested parentheses. I have to admit I'm not readily familiar with any of them, since they quickly get more complicated to use than just writing some looping logic around a much simpler set of matches.

Match just "1 and 2 or 4", no parentheses:
^\d+(?:\s*(?:AND|OR)\s*\d+)*$
Next, instead of each \d+, also allow the same expression, wrapped with parentheses:
^(?:\d+|\(\d+(?:\s*(?:AND|OR)\s*\d+)*\))(?:\s*(?:AND|OR)\s*(?:\d+|\(\d+(?:\s*(?:AND|OR)\s*\d+)*\)))*$
OK - it isn't beautiful, but it works. Obviously, this assume just a single level of parentheses.
As the comments say, depending on your language and requirements you may find a nicer solution.
Working example: http://www.debuggex.com/r/eMBWubl5yAp6hUqQ

You need to parse it with looping with my solution. loop this regex (\({0,1}\d+ (OR|AND) \d+\){0,1}) and repalce all the match value into digit(like 1). until none in the text match (\({0,1}\d+ (OR|AND) \d+\){0,1}). if you have only digit then that text is valid, if not it doesn't valid.

Related

How do I simplify that regex? [duplicate]

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
Closed 2 years ago.
I'm trying to get a remaining time data from a text but the times are written as months, weeks, days or hours rather then number. I've written this regex but it's a bit complicated. How can I simplify it?
[0-99] month[s]?|[0-99] week[s]?|[0-99] day[s]?|[0-99] hour[s]?
Example output:
2 days 4 hours
[0-99] is equivalent to a character set from 0 to 9, plus the character 9 - so it's equivalent to [0-9] - which is (often) equivalent to \d.
A character set with a single character in it is superfluous - just use the single character.
Finally, since the only thing that changes between the alternations is the word, put a group around the word and alternate inside the group:
\d (?:month|week|day|hour)s?\d
That's equivalent to your original pattern. But it sounds like you might be wanting to match up to 2 digits instead, in which case you can tweak it to:
\d{1,2} (?:month|week|day|hour)s?\d{1,2}

Regex capture groups of N and the remainder? [duplicate]

This question already has answers here:
Split large string in n-size chunks in JavaScript
(23 answers)
Closed 2 years ago.
Supposing, I have a string ASDFZXCVQW, is it possible to capture this into groups of N, and then the remaining characters would be in the final group.
For example, if N were 4, then we could have: ASDF, ZXCV, and QW. Notice how the QW is everything that is left over.
I know how to capture the groups of N with .{N}, and then manually get the leftover through string indexing, but is it possible to do this in a single regular expression?
var data = 'ASDFZXCVQW'
var result = data.match(/\D{1,4}/g)
console.log(result)
It will be helpful!
That really depends on the language in use.
In general, it will be a concatenation of 0 or more 4-character groups followed by 0-3 single characters.
Here is a possible formal definition for alphanumeric string: ([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])*[a-zA-Z0-9]*. Different languages might express this differently and possibly in a more compact way.

Regex value of 0.0-5.0 [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 5 years ago.
I'm trying to create a regex string that allows values 0.0 - 5.0. I need the one decimal point to be required. The string below gets me there, but it also allows 5.1-5.9. How do I prevent 5.1-5.9 from being entered, and allow 5.0?
^[0-5]+(\.[0-9]{1})$
Try this regex:
^([0-4]\.[0-9]|5\.0)$
It matches any number from 0 to 4 then dot then any number.
it also matches 5.0
Note: Your regex has another problem that you used + after [0-5] which also matches 55 for example, so you need to remove the +. You also need to remove {1}, It won't make any change but it's useless.

How to create a Regular Expression that includes all strings of 1 2 and 3 with no more than four 2's?

I'm looking for something like this: 1* 24 3*
But that doesn't take into account different order like the string 231. Any ideas how to specify the order using regular expressions?
I do not know what your intention was with 1^* 2^4 3^*, but going from your description,
/^([13]*2){0,4}[13]*$/
will match any combination of 1, 2 and 3, but with no more than four of 2.
However, you did not specify the regexp engine, so it might not work in some (notably, those without the {} repetition operator), in which case you will need to unpack it.
EDIT: Having seen tripleee's comment, I finally understood the original attempt. Anyway, in formal regular expression, you definitely need to unpack (not only you don't have {}, you don't even have x? as shorthand for (x|ε)):
(1|3)*
(2 (1|3)*
(2 (1|3)*
(2 (1|3)*
(2 (1|3)*
|ε)
|ε)
|ε)
|ε)
If I understood your requirement correctly you want to allow all combinations of 1,2,3 with restriction that digit 2 shouldn't be there more than 4 times.
You need to use lookahead for this regex:
^(?!(.*?2){5,})[123]+$
Online Demo: http://regex101.com/r/dP2bK3

is it possible to do Math on Variable Values in Regex? [duplicate]

This question already has answers here:
Math operations in regex
(3 answers)
Closed 8 years ago.
i am wondering of it is possible to do some simple Math on RegEx Variable values.
E.G.:
I am looking for all two-digit numbers is a textfile and would like to multiply them by 10.
Is simple regex able to do this or do i need to use a more complex script for that?
thanks!
Multiply two-digits number is like appending 0 at the end of the numbers. So that can be done with any regular expression that support replace and capturing group.
For example, here is Python code:
>>> re.sub(r'\b(\d{2})\b', r'\g<1>0', 'There are 10 apples.')
'There are 100 apples.'
But what you want is multiply by arbitrary number, then you need the regular expression engine that support some kind of callback / evaluation.
>>> re.sub(r'\b(\d{2})\b', lambda m: str(int(m.group(1)) * 5), '10 apples.')
'50 apples.'