2

I'm trying to write regular expression to restrict empty string and comma inside a string

example:

string = "" # not allowed
s = "ab c!@#)(*&^%$#~" # allowed
s = "sfsfsf,sdfsdf" # not allowed

The intention of this regexp is to do a pattern matching in swagger documentation like this property: type: string pattern: "^[0-9A-Za-z_]+$" value: type: object

I tried this regular expression ^(?!\s*$)[^,]*$ but it is not supported inside swagger doc and I get this error in golang code for this regular expression:

invalid or unsupported Perl syntax: `(?!

xgord
  • 4,606
  • 6
  • 30
  • 51
Shashank Bhat
  • 101
  • 2
  • 11

1 Answers1

3

You actually don't need a look ahead.

Just remove that lookahead expression and change * to + which will require at least one character and simplify your regex to ^[^,]+$

Let me know if that works or if you have more conditions.

Edit:

Considering @JvdV's comment (and he seems right), you can use following regex,

^\s*([^,\s]+\s*)+$

This will match if there is at least one non whitespace character and whole string will be rejected if there is even a single occurrence of a comma

Check this demo

Edit1:

To avoid catastrophic backtracking, the regex can be simplified as following,

^\s*[^,\s][^,]*$

Explanation:

  • ^ - Match start of input
  • \s* - Optionally match any whitespaces
  • [^,\s] - Match at least one character other than comma and whitespace
  • [^,]* - Can be followed by any character other than comma zero or more times
  • $ - Match end of string

Updated Demo

Edit2: For avoiding leading/trailing whitespaces, you can use this regex,

^[^\s,]([^,]*[^\s,])?$

Demo avoiding leading/trailing whitespaces

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • 2
    By the looks of ops attempt he want to avoid an empty string or rather a string with just whitespace. Maybe `^[^,]*[^,\s][^,]*$` is more comprehensive. – JvdV Aug 13 '22 at 19:45
  • @JvdV: Yes you seem right. Updated answer to take care empty string to not match – Pushpesh Kumar Rajwanshi Aug 13 '22 at 19:56
  • @PushpeshKumarRajwanshi this will cause catastrophic backtracking if string is big and comma comes at the end https://regex101.com/r/Ff01w6/1 – Shashank Bhat Aug 22 '22 at 12:29
  • 1
    @ShashankBhat: Yes, overly nesting can be avoided and you can use `^\s*[^,\s][^,]*$` regex. Let me update answer. – Pushpesh Kumar Rajwanshi Aug 22 '22 at 19:31
  • @PushpeshKumarRajwanshi Thanks for the answer. Just to make it much simpler, `^[^\s,][^,]*[^\s,]$` This will not allow trailing whitespaces. Correct me if Im wrong – Shashank Bhat Aug 22 '22 at 20:08
  • @ShashankBhat: In that case, your regex will require minimum two characters to match, where as a single character should match too. If you don't want to allow leading/trailing whitespaces, then go for this regex `^[^\s,]([^,]*[^\s,])?$` [See this demo](https://regex101.com/r/N4CG1J/1) and this matches just single character too. – Pushpesh Kumar Rajwanshi Aug 23 '22 at 19:11