From: 7stud -- Date: 2009-09-20T13:58:49+09:00 Subject: Re: Regexp question difference between ^ and \A Hunt Jon wrote: > Hi > > I'm trying to find good use examples to show a difference > between ^ and \A in Ruby regular expressions. > > \A is for beginning of a string > ^ beginning of a line or string > > As a regular expression runs one line by line (if I'm correct), > I think both are the same. > What does "runs one line by line" mean? A regular expression may examine the whole darn string--character by character. For example: str = "abc\n123" results = str.scan(/./m) p results --output:-- ["a", "b", "c", "\n", "1", "2", "3"] Do you consider that "runs one line by line"? \A matches just before the *FIRST* character of a string--and nowhere else: str = "abc\n123" results = str.scan(/\A.../) p results --output:-- ["abc"] ^ matches just before the *first* character of a string, *AND* just after any newline: str = "abc\n123" results = str.scan(/^.../) p results --output:-- ["abc", "123"] If your string doesn't have any newlines, guess what? str = "abc123" results = str.scan(/\A.../) p results --output:-- ["abc"] str = "abc123" results = str.scan(/^.../) p results --output:-- ["abc"] ... \A and ^ are equivalent. -- Posted via http://www.ruby-forum.com/.