我想查看句子中有多少关键字
usually
,drink
usually
usually
,drink
usually
和drink
if
,fly
,where
,go
if
,where
和go
我知道正则表达式,但我不知道如何使用它。我期待着你的宝贵回复。
答案 0 :(得分:1)
正则表达式是一种与字符串匹配的模式。
正则表达式模式与字符串中的任何匹配项匹配。
这种模式可以是很多像#34;所有数字","以大写字母开头,然后全部小写"还有更多。
您正在寻找的运营商管道|
与(this|if|key|<keyword>)
您可以在线测试正则表达式@ regex101.com
C#中的简单正则表达式
Regex rx = new Regex("<my_pattern>");
var matches = rx.Matches("<my_string>");
foreach(Match match in matches)
{
// do whatever you like here with the match.
}
注意:还有其他方法可以在字符串中查找多个关键字,速度更快,但我相信正则表达式是最简单的,对初学者来说,最合适。
答案 1 :(得分:0)
好吧,尝试一步一步解决问题步骤:
将首句分成单词。如果我们将word声明为字母字符的子字符串,我们可以使用SQL*Plus: Release 11.2.0.1.0 Production on Tue Nov 28 15:29:43 2017
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Enter user-name: scott
Enter password:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> desc empdetails;
Name Null? Type
----------------------------------------- -------- ----------------------------
ENO NUMBER(38)
ENAME VARCHAR2(20)
SAL FLOAT(126)
SQL> insert into empdetails values(1010,'John',45000.00);
1 row created.
SQL> commit;
Commit complete.
:
Regex
组织 string sentence = "What do you usually drink?";
//TODO: what if word can include an apostroph, e.g. "its'"?
var words = Regex
.Matches(sentence, @"\w+")
.OfType<Match>()
.Select(match => match.Value);
// Check yourself:
// Console.Write(string.Join(", ", words));
;让他们不区分大小写:
keywords
从 HashSet<string> keywords = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"usually",
"drink",
};
// Check yourself:
// Console.WriteLine((keywords.Contains("Drink") ? "keyword" : "plain word"))
过滤掉关键字:
words
打印出 var result = words
.Where(word => keywords.Contains(word))
.ToArray();
:
result
结果:
Console.WriteLine(
$"\"{sentence}\" contains {result.Length} keywords: [{string.Join(", ", result)}]");