1

I want to make my form field input to pass through a validator to allow only alphabets number and three symbols - ' / to pass.

r'^[A-Za-z0-9\s-/]+$';

I have done for all except for symbol ' . Once I add in ' symbols it will assume I close the statement on there. How can I put in the symbols ' .

8
  • Escaping ' like \' should work. Commented Aug 25, 2022 at 9:23
  • Sadly is not working \ ' the line will detect it as close parentheses Commented Aug 25, 2022 at 9:38
  • Use the same as @PushpeshKumarRajwanshi said. But start and close string expression with r"" Commented Aug 25, 2022 at 9:59
  • Can you try enclosing whole regex in doublequotes instead of singlequote and then put singlequote inside? Commented Aug 25, 2022 at 10:00
  • I change the line to r"^[A-Za-z0-9\s\'-/]+$" and the regex works for still only - /, the ' symbol still get eliminated Commented Aug 25, 2022 at 10:05

2 Answers 2

1

If that singlequote is still bothering you and nothing else worked, then there is another way to achieve it. A little tedious way but works pretty well.

Try using below regex,

^[^\u0000-\u001f\u0021-\u0026\u0028-\u002c.\u003a-\u0040\u005b-\u0060\u007b-\uffff]+$

Basically this regex excludes the character ranges that are not valid in your character set. I can add detailed explanation once you confirm it works for you and it should as it doesn't have any singlequote in the regex which was causing problem.

Had to use Unicode notation to prohibit matching Unicode characters.

Check this demo for valid and invalid matches

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

1 Comment

I have tried this regex. Really appreciate your ans but sadly it not working. However it gives my idea that Unicode characters, which I found the three different types of single quotes (back ,front ,straight) in it. I then able to solve the problem. r"^[A-Za-z0-9\s/\'\‘\’-]+$"
1

First of all, always place - at the end of the character class, it is the safest method to use it inside brackets.

Next, adding ' to a single-quoted string literal is done with an escape single quote, \'. Since this does not work, I suspect the problem is that you have curly quotes.

Also, consider using triple-quoted string literals, r"""<pattern>""". This is the most convenient way of writing patterns with quotes.

So you can consider using

pattern = r'''^[A-Za-z0-9\s/'‘’-]+$'''

If there is some warning you get, escape these special chars

pattern = r'''^[A-Za-z0-9\s\/\'\‘\’\-]+$'''

1 Comment

by escaping this three types of single quotes it works in my side. Thanks a lot.

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.