-1

I am handling user input in my program by using regular exp.

  1. the string contains /_MyWord/ and only a-z is accepted before /_MyWord/.
  2. the string not contain /s/123, /s/32A and atr/will in the beginning.

My try:

^(?!.*/s/123)(?!.*/s/32A )(?!.*atr/will)([/a-z]+)/_MyWord/(.*)$

Example:

/s/123/QWERERTYU/_MyWord/45454545 -> fail
/DFGH/FGHJK/GHJK/_MyWord/DFGHJ452 -> OK
HiCanYouHelpMe/_MyWord/fgh        -> OK
/_MyWord/HiCanYouHelpMefgh        -> OK

Can anyone help me to finish the Regular Exp string

MRWonderFuXker
  • 203
  • 3
  • 19

1 Answers1

1

If I got your question correctly, try this regex:

^(?!.*\/s\/123)(?!.*\/s\/32A)(?!.*atr\/will)([\/a-zA-Z]*)\/_MyWord\/(.*)$

Unescaped: ^(?!.*/s/123)(?!.*/s/32A)(?!.*atr/will)([/a-zA-Z]*)/_MyWord/(.*)$

  • Changed ([\/a-z]+) to ([\/a-zA-Z]*) to include lower and upper case as well as support none (e.g /_MyWord/Test)

Regex101 Demo

Works for

/DFGH/FGHJK/GHJK/_MyWord/DFGHJ452
HiCanYouHelpMe/_MyWord/fgh
/_MyWord/HiCanYouHelpMefgh

Doesn't match:

/s/123/QWERERTYU/_MyWord/45454545
atr/will/DFGH/FGHJK/GHJK/_MyWord/DFGHJ452

Also, you really don't need lookaheads for /s/123 and /s/32A since they contain numbers so they will automatically be rejected because your condition includes [a-zA-Z]. So you might want to remove (?!.*\/s\/123)(?!.*\/s\/32A) from the beginning.

degant
  • 4,861
  • 1
  • 17
  • 29