datalost
datalost

Reputation: 3773

Regex getting text inside

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.

Upvotes: 2

Views: 126

Answers (5)

Fernando Briano
Fernando Briano

Reputation: 7757

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.">

Upvotes: 1

derp
derp

Reputation: 3030

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

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236004

Try this:

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

Upvotes: 0

Larsenal
Larsenal

Reputation: 51156

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

Upvotes: 1

FailedDev
FailedDev

Reputation: 26930

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.

Upvotes: 4

Related Questions