简体   繁体   English

提取正则表达式中的特定单词

[英]Extract specific words in regular expression

I'm struggling a little with a regular expression here. 我在这里用正则表达式有点挣扎。

I have a sentence like this: "what is the date of birth of John Smith". 我有这样一句话:“约翰·史密斯(John Smith)的生日是什么”。 I would like to extract the keywords [date of birth] but I'm not sure how to do it since the word of is also present later in the sentence. 我想提取关键词[生日],但是由于该词的后面也出现了'我'字样,所以我不确定该怎么做。

Thanks, Gwendal 谢谢,Gwendal

If you want only to check the presence of the string date of birth then use the String contains(CharSequence s) method. 如果只想检查字符串date of birth则使用String contains(CharSequence s)方法。

Usage 用法

String str="what is the date of birth of John Smith";
str.contains("date of birth"); // Returns in Boolean whether the char sequence is present in s

If you want to know the first occurrence of the specified substring use int indexOf(String str) method 如果您想知道指定子字符串的首次出现,请使用int indexOf(String str)方法

Usage 用法

String str="what is the date of birth of John Smith";
str.indexOf("date of birth"); // Returns the first pos of occurence of the substring

Check this page for more information on String 检查此页面以获取有关String的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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