简体   繁体   中英

How i can extract words from strings?

I'm using Ruby 1.9.3, and want to extract "Post" and "Topic" words from these strings:

"[MediaExecsTech] New Topic Creation in Open Technology forum"
"[MediaExecsTech] New Post Creation in Open Technology forum"

Is there a regex I can use?

There are lots of ways you can either find whether the strings contain "Topic" or "Post", printing that out, or returning the information for further processing, or using some logic to process them individually.

Here are some various ways I might do it:

REGEX = /\b(#{ Regexp.union(%w[Topic Post]) })\b/
=> /\b((?-mix:Topic|Post))\b/

ARRAY = [
  "[MediaExecsTech] New Topic Creation in Open Technology forum",
  "[MediaExecsTech] New Post Creation in Open Technology forum"
]

ARRAY.each do |s|
  puts s[REGEX, 1]
end
=> Topic
=> Post

That just prints the word found.

ARRAY.map { |s|
  s[REGEX, 1]
}
=> [
     [0] "Topic",
     [1] "Post"
   ]

That returns an array for each string searched. If the words don't appear the array element would be nil .

ARRAY.each do |s|
  case s[REGEX, 1]
  when 'Topic'
    puts "#{ s } contains Topic"
  when 'Post'
    puts "#{ s } contains Post"
  end

  case s
  when /\bTopic\b/i
    puts "#{ s } contains Topic"
  when /\bPost\b/i
    puts "#{ s } contains Post"
  end
end
=> [MediaExecsTech] New Topic Creation in Open Technology forum contains Topic
=> [MediaExecsTech] New Topic Creation in Open Technology forum contains Topic
=> [MediaExecsTech] New Post Creation in Open Technology forum contains Post
=> [MediaExecsTech] New Post Creation in Open Technology forum contains Post

These just print out the string and whether "Topic" or "Post" was found. Instead of printing, you could do further processing.

This will extract the Topic or Post title:

thestring.match(/New (Topic|Post) (.+)/)[2]

I believe I misunderstood your question. I take it you literally want the words "post" and "topic". In which case something like what joeframbach suggested should work:

thestring.match(/post|topic/i)[0]
yourstring.match(/post|topic/i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM