Pattern matching in PHP


Test of pattern matching

text = Inu mo aruke ba boo ni ataru.
pattern = /ba/
matches[0] (((ba)))
matches[1] ((()))
matches[2] ((()))

text = Inu mo aruke ba boo ni ataru.
pattern = /n.+n/
matches[0] (((nu mo aruke ba boo n)))
matches[1] ((()))
matches[2] ((()))

text = Inu mo aruke ba boo ni ataru.
pattern = /n(.+)n/
matches[0] (((nu mo aruke ba boo n)))
matches[1] (((u mo aruke ba boo )))
matches[2] ((()))

text = Heta no yokozuki.
pattern = / ([^ ]*) /
whole pattern matches _word_
parenthesized pattern matches word
matches[0] ((( no )))
matches[1] (((no)))
matches[2] ((()))

text = Heta no yokozuki.
pattern = / ([^ ]*) ([^ ]*)\./
whole pattern matches _word_word.
each parenthesized pattern matches word
matches[0] ((( no yokozuki.)))
matches[1] (((no)))
matches[2] (((yokozuki)))
matches[3] ((()))

text = Can the patterns be nested?? Yes!!
pattern = / (([^ ]*) ([^ ]*)) /
the pattern structure is (()())
whole pattern matches _word_word_
each parenthesized pattern matches word
and the enclosing parentheses match word_word
matches[0] ((( the patterns )))
matches[1] (((the patterns)))
matches[2] (((the)))
matches[3] (((patterns)))
matches[4] ((()))
matches[5] ((()))