English 中文(简体)
是否有与载有A但不含B[复制]的体格比法。
原标题:Is there a regex to match a string that contains A but does not contain B [duplicate]

My problem is that I want to check the browserstring with pure regex.

Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13

1. 适当

Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 

不适用

My tried solution is: /?((?<=Android)(?:[^])*?(?=Mobile))/i but it matches exactly wrong.

问题回答

如果扼杀含有一个字句,请先检查。

如果你想保证,在某个地方,扼杀含有“roid”,你可以这样做:

^(?=.*Android).*

你们也可以把它们结合起来,确保其中某些地方包含“roid”和“Mobile”:

^(?=.*Android)(?=.*Mobile).*

如果你想确保,在扼杀中某个词是NOT,则使用前面的负面看法:

^(?=.*Android)(?!.*Mobile).*

This would require the word "Android to be in the string and the word "Mobile" is not allowed in the string. The .* part matches then the complete string/row when the assertions at the beginning are true.

See it here on Regexr

With some implementations of regular expressions, you can use a negative lookbehind assertion. Per the docs, a negative lookbehind written as (?<!...) matches only if the current position in the string is not preceded by a match for ...

这里有一份互动文字,显示如何用你的抽样插图来进行负面研究:

>>> s = "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13"
>>> bool(re.search(r Android.*(?<! Mobile) Safari , s))
True

>>> t = "Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
>>> bool(re.search(r Android.*(?<! Mobile) Safari , t))
False

I d just break it up

if ((m/Android/i) && (m/Safari/i) && !(m/Mobile Safari/i))

既然这样的话,根据斯拉夫,你可以把这一点结合起来。

if ((m/Android/i) && (m/(?<!Mobile )Safari/i))

or even

if (m/Android.*(?<!Mobile )Safari/i)

FYI see Lookahead/lookbehind


Update Tested these fine with Perl5 regex flavour (arguably the most popular flavour):

perl -ne  print "$. yes
" if m/Android.*(?<!Mobile )Safari/i 

表演:

1 yes

附属履行机构





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签