ruby - Why does having a literal space between regex tokens lead to different matchdata objects? -


for example, consider following expressions:

no_space = "this test".match(/(\w+)(\w+)/)  with_space = "this test".match(/(\w+) (\w+)/)  

the expression no_space matchdata object #<matchdata "this" 1:"thi" 2:"s">, while with_space #<matchdata "this is" 1:"this" 2:"is">. going on here? seems me literal space between tokens indicates ruby should match multiple words if possible, while not having space causes match limited 1 word. explanation or clarification on subject appreciated.

thanks.

\w doesn't match space, , + greedy unless follow ?, ruby tries match many \w possible, long rest of express matches, consuming thi in first capture, , s in second.

when add space, ruby matches many \w until space character, , many \w, therefore matching this , is.

please let me know if isn't clear.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -