How to use Regex to exclude words?
Can anyone help me out with a Regex that will exclude words th开发者_如何学JAVAat are inside:
title = "EXCLUDE ANYTHING HERE"
.
well, with "regex" you won't exclude nothing. You can use a programing language or editors (vi or sed for example) to match this regex and delete the matched text for you.
what i understood is. You want to delete all UPPERCASE Letters after "title=" right?
with ruby you can do something like that
a = ["title=AAA","title=bbb","title=CCC"]
x = a.collect {|l| l unless l.split('=')[1] =~ /^[A-Z]+$/ }.compact
at x you will have just the "title=bbb" as you wanted.
Shorter:
a = ["title=AAA","title=bbb","title=CCC"]
x = a.delete_if { |s| s.match(/=[A-Z]+$/) }
More Rubyish*:
titles = ["title=AAA","title=bbb","title=CCC"]
titles.reject! do |item|
item.ends_with_caps?
end
class String
def ends_with_caps?
self.match /[A-Z]+$/
end
end
*sarcasm/exaggeration
精彩评论