4

For example, if I have the following strings:

99%89 (should match)
99%? (should match)
?%99 (should match)
?%? (should not match)
?%99%99 (should match)
99%99%99%? (should match)

essentially the first or second element can be a ? or a number, but both elements cannot be ?. I tried thinking of something like:

[0-9]*|[?](?!\?)[%][0-9]*|[?]

But this does not yield the correct answer, any help would be appreciated

4
  • The ? in the negative look ahead needs to be escaped... or I think. Not really clear what the question is. There is no & in the sample strings. Commented Apr 11, 2021 at 1:24
  • 1
    sorry, I edited the question to make it more understandable. even after escaping the ?, it still matches ?%? Commented Apr 11, 2021 at 1:34
  • 1
    And it shouldn't? Please define what should and shouldn't match, and what it currently does. Question is still not clear to me. Also where regex will be used may be useful. PCRE can do things POSIX can't etc. Commented Apr 11, 2021 at 1:36
  • 2
    I updated the question again; this regex is used in java Commented Apr 11, 2021 at 2:52

2 Answers 2

5

With your shown samples, could you please try following.

^(?:(?:\?(?:(?:%\d+){1,})?)|(?:(?:(?:\d+%){1,})?\?(?:(?:%\d+){1,})?)|(?:\d+%\d+))$

Online demo for above regex

Explanation: Adding detailed explanation for above.

^(?:                       ##Matching from starting of the value, starting a non-capturing group from here.
 (?:\?                     ##Starting non-capturing group(one for understanding purposes) matching literal ? here.
    (?:(?:%\d+){1,})?      ##In a non capturing group looking for % with 1 or more occurrences of digits and matching this group match keeping it optional.
  )|                       ##Closing one non-capturing group here, with OR condition here.
 (?:                       ##Starting non-capturing group(two) here.
    (?:(?:\d+%){1,})?\?    ##Looking for digits with % one or more occurrences in a non-capturing group keeping it optional followed by ?
    (?:(?:%\d+){1,})?      ##Checking for % digits one or more occurrences in a non-capturing group keeping it optional followed by ?
 )|                        ##Closing two non-capturing group here, with OR condition here.
 (?:\d+%\d+)               ##In a non-capturing group looking for 1 or more digits % one or more digits
)$                         ##Closing  1st non-capturing group at the end of value.
Sign up to request clarification or add additional context in comments.

3 Comments

@AdamantiumPrime, you mean more than 1 instances(continuous ones)? Could you please give me a single sample which you tried on? Because with your shown samples my code has worked perfectly fine, let me know.
@AdamantiumPrime, How about using ^(?:(?:\?(?:(?:%\d+){1,})?)|(?:(?:(?:\d+%){1,})?\?(?:(?:%\d+){1,})?)|(?:\d+%\d+))$ once? I tested it with samples like: 9%?%100%100 99%89 99%? ?%99 ?%? ?%99%99 99%99%99%? Let me know how it goes? See its regex DEMO once regex101.com/r/lLkGe4/1 and let me know then.
@AdamantiumPrime, Your welcome, could you please update your samples(since you have requested to edit regex for few other cases too, not sure why that comment got deleted) in your question so that it will be clear for people, for understanding purposes, I have updated my answer and explanation of it as well now.
3

Not sure if I am reading the question right, but as you tried using a negative lookahead you could assert that the string does not only contains % and/or ?

^(?![%?]+$)[\d?%]+$

Regex demo

Or without a lookahead:

^[%?]*\d[%?\d]*$

Regex demo

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.