正则表达式获取内部文本

发布于 2024-12-09 21:44:15 字数 344 浏览 3 评论 0原文

我一直试图在句子中获取第一个位置名称。所需的位置名称将恰好从第一个句子的第二个大写字母开始,然后恰好在第一个点(。)之前

结束示例:

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

所需的输出

最高法院

抱歉,我无法向您展示我目前掌握的一段代码。经过一个小时的尝试,我没有得到任何具体的结果。

如果您能用 Ruby 展示代码示例,我们将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

影子是时光的心 2024-12-16 21:44:15

这个正则表达式:

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

将产生:Supreme Court

我从匹配第一个大写字母的字符串开头开始,而忽略其他一切。然后,我匹配第二个大写字母并将结果保存到反向引用 1 中,直到第一个点。

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.

唯憾梦倾城 2024-12-16 21:44:15
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
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
三生殊途 2024-12-16 21:44:15

这对我有用:

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

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.">
嘦怹 2024-12-16 21:44:15

试试这个:

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

Try this:

s = 'It is located at Supreme Court. Follow by some other text.'
/[A-Z].+?([A-Z].*)\..+?/.match(s)[1]
御弟哥哥 2024-12-16 21:44:15

这假设字符串的开头没有空格,因此它会查找紧随空格之后的第一个大写字母并抓取任何内容,直到找到第一个句点。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文