开发者

Regex getting text inside

开发者 https://www.devze.com 2023-04-13 02:10 出处:网络
I have been trying to grab a first location name inside the sentences. The desired location name will exactly starts at the 2nd capital of the 开发者_开发知识库first sentence and then precisely end be

I have been trying to grab a first location name inside the sentences. The desired location name will exactly starts at the 2nd capital of the 开发者_开发知识库first sentence and then precisely end before the first dot(.)

Example:

 It is located at Supreme Court. Follow by some other text. 
                  ^           ^

Desired out put

Supreme Court

Sorry I can't show you a piece of code that I've got so far. After an hour of trying, I got nothing in concrete.

If you show the code example in Ruby would be highly appreciated.


This regex :

regexp = /^.*?[A-Z].*?([A-Z].*?)\./
match = regexp.match(subject)
if match
    match = match[1]
else
    match = ""
end

Will produce : Supreme Court

I start from the start of the string matching the first capital while ignoring everyhting else. I then match the 2nd capital and save the result into backreference 1 until the first dot.


s = 'It is located at Supreme Court. Follow by some other text.'
m = s.match /[A-Z][^A-Z]+([A-Z][^\.]+)/
result = m[1] #Supreme Court


This worked for me:

irb(main):001:0> location = "It is located at Supreme Court. Follow by some other text."
=> "It is located at Supreme Court. Follow by some other text."
irb(main):002:0> location.match(/[^A-Za-z][\bA-Z][\w\s]*\./)
=> #<MatchData "Supreme Court.">


Try this:

s = 'It is located at Supreme Court. Follow by some other text.'
/[A-Z].+?([A-Z].*)\..+?/.match(s)[1]


This assumes there's no space at the beginning of the string, therefore it looks for the first capital letter that comes right after a space and grabs anything until the first period it finds.

str = "It is located at Supreme Court. Follow by some other text."
m = str.match(/\s([A-Z].*?)\./)
location = m.nil? ? "" : m[1] #just in case there's no match

p location #=> Supreme Court
0

精彩评论

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

关注公众号