正则表达式获取内部文本
我一直试图在句子中获取第一个位置名称。所需的位置名称将恰好从第一个句子的第二个大写字母开始,然后恰好在第一个点(。)之前
结束示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个正则表达式:
将产生:
Supreme Court
我从匹配第一个大写字母的字符串开头开始,而忽略其他一切。然后,我匹配第二个大写字母并将结果保存到反向引用 1 中,直到第一个点。
This regex :
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.
这对我有用:
This worked for me:
试试这个:
Try this:
这假设字符串的开头没有空格,因此它会查找紧随空格之后的第一个大写字母并抓取任何内容,直到找到第一个句点。
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.