1

I was trying to understand the following regex

^(?!\s)(?!.*\s\s)[ A-Za-z0-9'-]{1,35}(?<!\s)$ 

Here I understood,

^(?!\s) - (lookahead) means will not match if there is a leading space.

[ A-Za-z0-9'-]{1,35} - will allow A to Z, a to z, 0 to 9, (space), ' and -.

(?<!\s)$ - means will not match if there is a trailing space.

I am unable to understand what this sub-expression is doing?

(?!.*\s\s)
NJMR
  • 1,710
  • 1
  • 21
  • 38

2 Answers2

1
(?!.*\s\s)

means that in the string there should not be 2 adjacent spaces.

.* will scan the string. \s\s will look for adjacent spaces.?! is negative lookahead so it will break when it finds such a string.See demo.

https://regex101.com/r/eB8xU8/4

vks
  • 63,206
  • 9
  • 78
  • 110
  • I did a sample application which takes regex and the subject string. for the given input "(?!.*\s\s)" "Abc Def" it is returning true. Note that there are 2 spaces between "Abc" and "Def". – NJMR Feb 10 '16 at 05:48
  • @NJMR u need to use `^(?!.*\s\s).*$` – vks Feb 10 '16 at 05:49
  • Taking the above example which is "Abc Def" (there are 2 spaces). My question is .* will consume the first space and you are left with only single space. Then the pattern should match the subject. Is my understanding correct? – NJMR Feb 10 '16 at 06:23
  • @NJMR .* will consume but then backtracks as it has to match \s\s ....so it will leave d two so that d match can happen – vks Feb 10 '16 at 06:32
-1

I think there is no effect for this lookup.. It will return always true because here you are using greedy search. It should be
(?!.*?\s\s): which means no two spaces continuous..

.* this is for match everything situation with out looking here and there
.* ? this makes this as a lazy operator and consider situation written at the right side of .*?

Sahil Gulati
  • 14,401
  • 4
  • 20
  • 39
  • Thanks for the reply. I have tried the regex for the following input. "Abc Def". The regex returns false. There are 3 spaces between "Abc" and "Def" – NJMR Feb 10 '16 at 04:16
  • @NJMR Tell me an exact string you want to match so that i can help you out.. The regular expression written above will fail for sure.. as it is not correct.. – Sahil Gulati Feb 10 '16 at 04:44
  • I dont want to match any string which had leading, trailing and adjacent spaces. The "^(?!\s)(?!.*\s\s)[ A-Za-z0-9'-]{1,35}(? – NJMR Feb 10 '16 at 04:47
  • @NJMR see if you use this .* it means match all without caring any thing.. and if you are using .*?\s\s it means match all but after checking the condition given at the right which is \s\s and (?!.*?\s\s) this whole means look ahead with and return false if it contains two times \s\s.... if you want to check it for more than two spaces use this (?!.*?\s{1,})... for referrence use this ... http://www.regexr.com – Sahil Gulati Feb 10 '16 at 05:08
  • This is incorrect.`.*` is greedy but it allows regex engine to backtrack.So this will work though `.*?` will be more efficient in some cases. – vks Feb 10 '16 at 05:21
  • @vks just for understanding i was trying to explain in that way.... if you look into my post i said .* is greedy.. and .*? makes is lazy – Sahil Gulati Feb 10 '16 at 05:25
  • `there is no effect for this lookup` is incorrect – vks Feb 10 '16 at 05:25
  • @vks Got it.. That was by mistake.. – Sahil Gulati Feb 10 '16 at 05:41