0

I'm now facing a trouble in building a SQL regex searching at runtime.

Somewhat its like:

  • User inputs searching string ( one ? represents for 1 character [a-z0-9]):

AA?001

Data:
AAA001
AAB001
AA7001
AA70012
AB0001

Result:
AAA001
AAB001
AA7001

At first, I was thinking about the way that splitting it by [+?], then the conditions will be something like:

Startwith('AA') AND Endwith("001") AND LENGTH = 5)

But this way is not gonna work with pattern: A?0?1. So, I would probably have to build a Regex at runtime, but do not have any clue to do that.

Any suggest/idea/recommend would be grateful.

2
  • 1
    You mean this AA.001 ? Add anchors if necessary, ^AA.001$ Commented Feb 9, 2015 at 4:50
  • ohh right, just have to replace ? to dot. I dont even know about . before. So new to REGEX. thank you @AvinashRaj Commented Feb 9, 2015 at 4:58

1 Answer 1

1
^AA[A-Z0-9a-z]001$

Try this.See demo.

https://regex101.com/r/wX9fR1/8

Quantifier: ? Between zero and one time

? does not match a character on its own.It works on character preceded by it.

Also add ^$ anchors to disallow a partial match.

^AA\S001$

If you want to match any character except space.

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

2 Comments

thank you, but the regex depends on what user inputs. Like: ???001 or A???01, ... So the way AvinashRaj mentioned above will work for me :)
@Kingcesc . will allow AA 001 too.Do you want to allow space too.Use \S if you want all but no space.

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.