在许多编程语言中,可以使用正则表达式来实现不区分大小写的单词边界。下面是一些常用编程语言的示例代码:
const regex = /\bword\b/i;
const text = "This is a Word example.";
console.log(regex.test(text)); // Output: true
import re
pattern = r"\bword\b"
text = "This is a Word example."
print(re.search(pattern, text, re.IGNORECASE)) # Output:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String regex = "\\bword\\b";
String text = "This is a Word example.";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println("Match found!");
} else {
System.out.println("Match not found!");
}
}
}
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string pattern = @"\bword\b";
string text = "This is a Word example.";
bool isMatch = Regex.IsMatch(text, pattern, RegexOptions.IgnoreCase);
Console.WriteLine(isMatch); // Output: True
}
}
这些示例代码使用正则表达式中的\b
来表示单词边界,并使用相应编程语言的选项(如i
或IGNORECASE
)来忽略大小写。
上一篇:不区分大小写的查询利用索引
下一篇:不区分大小写的多个列表与列比较