Robert Klemme
3/8/2005 11:17:00 AM
<arfinmail@yahoo.com> schrieb im Newsbeitrag
news:1110232059.620437.152870@o13g2000cwo.googlegroups.com...
> I was trying to extract the file name from a string like this:
>
> aLine = "c:\\temp\\myFile.txt"
>
> I found a regular expression for pearl which I modified to /[^\\]+$/
> and it worked. Can anyone please explain this expression? I really
> can't make sense of it :(
It extracts a string of non backslashes "[^\\]+" anchored at the end of
the sequence "$".
> The way I look at it it's searching for the beginning of the file or a
No, the caret "^" inside the character group "[]" stands for "negation",
i.e., all characters *not* listed in the group. "[^0-9]" stands for "all
characters that are not digits".
> slash, immediatly followed by the end of the string. But it's clearly
> not what's happening.
>
> I'm going to look for a way of doing it with the File object for
> learning purposes :)
File#basename is the method you want.
> Another question: Given a string, is there a way to get all characters
> between XXX and YYY but only if it does not contain the pattern ZZZ in
> the middle?
> Ex:
> "This-XXX-is-an-YYY-example." => True
> "This-XXX-is-ZZZ-an-YYY-example." => False
>> s="This-XXX-is-an-YYY-example."
=> "This-XXX-is-an-YYY-example."
>> s.scan(/XXX(.*)YYY/)
=> [["-is-an-"]]
>> s.scan(/XXX(.*)YYY/).flatten.reject {|m| /ZZZ/ =~ m}
=> ["-is-an-"]
>> s.scan(/XXX(.*(?!ZZZ).*)YYY/).inject([]){|ar,m| ma=m[0];ar << ma unless
/ZZZ/=~ma;ar}
=> ["-is-an-"]
>> s="This-XXX-is-ZZZ-an-YYY-example."
=> "This-XXX-is-ZZZ-an-YYY-example."
>> s.scan(/XXX(.*)YYY/).flatten.reject {|m| /ZZZ/ =~ m}
=> []
>> s.scan(/XXX(.*(?!ZZZ).*)YYY/).inject([]){|ar,m| ma=m[0];ar << ma unless
/ZZZ/=~ma;ar}
=> []
Kind regards
robert