开发者

How to use Regex to exclude words?

开发者 https://www.devze.com 2023-01-14 20:29 出处:网络
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 programin

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

0

精彩评论

暂无评论...
验证码 换一张
取 消