【发布时间】:2011-10-02 04:08:25
【问题描述】:
如果给定的字符串不只包含给定的单词(例如“any”),我想用 Python 或 JavaScript 编写一个正则表达式来匹配。
例如:
any:不匹配AnY:不匹配anyday:匹配any day:匹配blabla:匹配
【问题讨论】:
-
我认为您需要更多示例。例如“Foobar”应该匹配吗?
标签: javascript python regex
如果给定的字符串不只包含给定的单词(例如“any”),我想用 Python 或 JavaScript 编写一个正则表达式来匹配。
例如:
any:不匹配AnY:不匹配anyday:匹配any day:匹配blabla:匹配
【问题讨论】:
标签: javascript python regex
类似这样的:
/(any)(.+)/i
【讨论】:
any.+
..还有一些文字来设置 30char 阈值
【讨论】:
如果您还需要以“any”开头的单词,则可以使用否定前瞻
^(?!any$).*$
这将匹配除“any”之外的任何内容。
【讨论】:
为此目的在 javascript 中使用 string.match(regexp) 方法。请看下面的代码:
<script type="text/javascript">
var str="source string contains YourWord";
var patt1=/YourWord/gi; // replace YourWord within this regex with the word you want to check.
if(str.match(patt1))
{
//This means there is "YourWord" in the source string str. Do the required logic accordingly.
}
else
{
// no match
}
</script>
希望这会有所帮助...
【讨论】:
不使用正则表达式可能更有效,这也有效:
def noAny(i):
i = i.lower().replace('any', '')
return len(i) > 0
【讨论】: