php - regex for a word but avoiding it if they are making another word
问题描述
Data:
Japan is beautiful country.
AU is the telecom company of japan, au networks are very good.
Japan is famous for good restaurants.
I need a RegEx to find the 'au'
or 'AU'
only even if have spaces before and after, but if it is within a word like 'beautiful'
and 'restaurants'
I want to avoid it.
Attempts
"/(?:au)/"
"/\P{au}/u"
"/(?:\A|\s)au(?:\s|\Z)/"
"/[^a-zA-Z]/"
if($word == 'au') {
}
解决方案
You can try this and just remove global handler if you want to search one time.
/(( )|^)(au)(( )|$)/gmi
I'm not sure what language you're using. I would match it using Perl with below method.
my $string = "Japan is beautiful country. AU is the telecom company of japan, au networks are very good. Japan is famous for good restaurants.";
my $matched;
if($string=~m/(( )|^)(au)(( )|$)/gmi) {
$matched = $2; ## This is AU, au Au or aU
}
## do something with $matched here
Sample of the match below.
推荐阅读
- php - 使用 php 转发电子邮件时出错
- javascript - 使用 react-datepicker 仅在选定的 div 中选择和更新日期
- c# - 使用 C# 的 Azure REST Api 身份验证
- typescript - EmptyError:在角度保护中使用 takeWhile 时没有序列中的元素
- html - 在Angular 6中折叠/展开嵌套的div
- mdx - MDX 将集合除以度量
- solr - 使用 Solr 自定义预输入
- bash - 如何在 sed 中处理 UTF-8 表情符号?
- javascript - 如何在按钮按下时触发警报功能?
- php - PHP/SQL - 查询关系数据库的 ID,但能够使用 ORDER BY 在单独的数据库中查询这些 ID