samedi 9 mai 2015

Matching across a line vs matching words regex

Why is it that when I match across new lines it would seem that I can't identify individual words. For example:

content = "COAL_STORIES
AUSTRALIA - blah blah blah
BOTSWANA – blah blah blah 

URANIUM_STORIES 
AUSTRALIA – blah
INDIA - blah

COPPER_STORIES
AUSTRALIA - blah blah blah
AUSTRALIA - blah blah blah
CHINA - blah blah blah

ALUMINIUM_STORIES"




sections = content.scan(/\w.*_.*\b/)

Give and array:

[
    [0] "COAL_STORIES",
    [1] "URANIUM_STORIES",
    [2] "COPPER_STORIES",
    [3] "ALUMINIUM_STORIES"
]

But if I try that using the 'm' flag everything gets matched:

sections = content.scan(/\w.*_.*\b/m) gives an array:

[
    [0] "COAL_STORIES\nAUSTRALIA - blah blah blah\nBOTSWANA – blah blah blah \n\nURANIUM_STORIES \nAUSTRALIA – blah\nINDIA - blah\n\nCOPPER_STORIES\nAUSTRALIA - blah blah blah\nAUSTRALIA - blah blah blah\nCHINA - blah blah blah\n\nALUMINIUM_STORIES"
]

As far as I can tell I'm still looking for the same word boundaries?

Aucun commentaire:

Enregistrer un commentaire