2

I am trying to check patterns in a string using regex. My requirements are

  1. The string should always start with % or (
  2. Immediately After % there should be a number and vice versa any number should be preceded by % always
  3. the string can only contain following characters and word % ( ) and or numeric value

Valid string (%1 and %2 or %3)

Invalid %%1

I tried the following

regex ^[%(]+[%0-9]+[(]

Please help

3
  • 1
    Looks like whitespace characters are OK too, right? Commented Dec 5, 2013 at 14:52
  • Yes whitespace characters are ok Commented Dec 5, 2013 at 14:53
  • Try regex ^[%(]+[%0-9\s]+[)] Commented Dec 5, 2013 at 14:55

4 Answers 4

3

This should do it:

^(?:\(|%[0-9]+)(?:\s+|%[0-9]+|and|or|\(|\))*$

Starts with an opening bracket or a percent then a series of numbers, then goes on to allow spaces or a percent followed by a series of numbers any number of times. If you give a more specific example of what you are trying to match I can be more specific

enter image description here

This does not force matching brackets. You need a recursive js function for that

EDIT

Alright, here is a more specific function to account for spacing and numbers around seperators

^(?:\(|%[0-9])(?:\sand\s|\sor\s|\(+)(?:(?:%[0-9]|\)+)(?:\s(?:and|or)\s(?:%[0-9]|(?:\(\s?)+))?)*\s?\)*$

This will match something like this: %3 and %4 or ( %2 or %3 ) but fails when you try it on: %3 and %4or ( %2 or %3 ) note the missing space between the %4 and the or

Again, brackets dont need to be closed so it matches: %3 and %4 or ( %2 or %3 and it also matches: %3 and %4 or ( %2 or %3 ))))

it can deal with immediately nested brackets like this: %3 and %4 or ((%2 or %3) and %9 )

Im sure I have missed cases, let me know what issues your run into

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, this works perfectly. Is it possible to explain the regex?
Of course. it has two parts each surrounded by (?: ). The ?: means the parens will not capture, it makes the regex a little bit faster and is useful if all you need to do is check if the regex matches not pull what it matches out of a string. inside the first brackets is (|%[-9]+. That matches either an opening bracket or a percent followed by one or more numbers. The second portion \s+|%[0-9]+|and|or|(|) matches one or more spaces or a percent followed by one or more digits or 'and' or 'or' or either bracket 0 or more times. Let me know if you want a specific explanation of something
Thanks a lot wolffer-east
Looking at Theox's answer below I forgot that you can combine the symbols into one or which would make the regex a bit smaller. Changing that out would give you (?:[\s()]+|%[0-9]+|and|or)* for the second portion of the regex
@Rakesh KR, how did you generate that image?
|
0

I would remove the white space from the string first just to make the regex easier.

This doesn't match exactly what you described, but matches what I think you're trying to do based on your example:

\(?%\d+\)?((and|or)\(?%\d+\)?)*

Note that it does not guarantee that the parentheses are all matched.

Comments

0

Here's a solution :

^(%\d|\()(%\d|and|or|[\s()])*$

This will match strings beginning with "%N", followed by any combination of "and", "or", whitespaces, ( ), and % followed by a number.

3 Comments

Good call on combining the symbols. I forgot that for a second there. You are only missing the option to start the string with an opening paren
@wolffer-east : Indeed, i changed that. I also saw that my regex match strings like "%1 andandandandandorororor" which is probably unwanted. I'll edit my post when I find how to fix this.
yeah, we would have to split out the %\d (or in my case %[0-9]) from the rest. Not sure if whitespace is required or not
0

try this, perhaps can help :

^[%][0-9]+|^[(][%][0-9]+[)]

http://RegExr.com?37h10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.