是什么意思(?!^) [英] What is the meaning of (?!^)

查看:619
本文介绍了是什么意思(?!^)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习正则表达式并感到困惑。
我看到这篇文章 java split()方法

所以我对Achintya Jha的第二个答案有一些疑问;

I am trying to learn regular expressions and got confused. I saw this post java split () method
so I have some questions regarding to the 2nd answer by Achintya Jha;


  1. 为什么 str2.split(); 给出 [,1,2,3]

  2. 检测到在文本的开头,如果是这样,为什么它最后不会这样做?

  3. 究竟是什么(?!^)是指?

  1. why does str2.split(""); give out as [, 1, 2, 3]
  2. does it detect "" at the start of the text and if so, why doesn't it do the same at the end?
  3. what exactly (?!^) means?

如果我没错_ a (?!b)返回 a 如果a后面没有 b

^ 查找必须在该行开头匹配的正则表达式,以便(?!^)获取空字符串

^ 找到必须在
行的开头匹配,所以返回如果这个后面没有

If I am not wrong a(?!b) returns a if a is not followed by b
and ^ finds regex that must match at the beginning of the line so (?!^) gets an empty string ""
and ^ finds "" that must match at the beginning of the line so returns "" if this "" is not followed by ""?

推荐答案

分裂开心在匹配正则表达式作为参数传递的地方。你需要知道,如果分裂发生,一件事就变成了两件事。总是。没有例外。

Split happens in places which matches regex passed as argument. You need to know that if split happens ONE thing becomes TWO things. Always. There is no exception.

你可以怀疑它因为实例abc.split(c)返回带有一个元素的数组 [ab] 但这是因为此版本的 split 还会在返回之前自动从数组中删除尾随的空字符串。

You can doubt it because of instance "abc".split("c") returns array with one element ["ab"] but that is because this version of split also automatically removes trailing empty strings from array before returning it.

换句话说abc.split(c)


  1. 创建 [ab,] 数组(是的,有空字符串,这是分裂<$ c的结果$ c>abc on c ),

  2. 删除尾随空字符串

  3. 返回结果数组,结尾没有那些空字符串,所以现在返回 [ab]

  1. creates ["ab",""] array (yes there is empty string which is result of splitting "abc" on c),
  2. removes trailing empty strings
  3. returns as result array without those empty strings at the end so now it returns ["ab"]

另一个例子是在a上拆分abc 。由于 a 在开始时出现,您将获得 [,bc]

Another example would be splitting "abc" on "a". Since a is present at start you will get ["", "bc"].

但是拆分空字符串有点棘手,因为空字符串在每个字符之前和之后。我将使用管道 | 标记它们。

But splitting on empty String is little bit more tricky, because empty string is before and after each characters. I will mark them using pipe |.

所以中的空字符串abc可以在这些位置找到| a | b | c |这意味着当你拆分abc时/ code> on

So empty Strings in "abc" can be found at these positions "|a|b|c|" which means that when you split "abc" on ""


  • 此方法(首先)生成数组 [,a,b,c,]

  • 以后删除尾随空字符串

这就是为什么abc.split()返回结果数组 [,a,b,c] (这应该回答你的问题1)。

That is why "abc".split("") returns as result array ["", "a", "b", "c"] (this should answer your question 1).

但是如果我们想要防止第一个空字符串(开头的那个)被split方法匹配怎么办?换句话说,如果我们不想拆分怎么办?

But what if we want to prevent first empty string (the one at start) from being matched by split method? In other words what if we don't want to split on

"|a|b|c|"

但仅限于

 "a|b|c|"

我们可以通过几种方式实现。

We can do it in few ways.


  1. 我们可以尝试创建正则表达式,它将匹配这些前面有任何字符的whatspace,如 a | b | c |

  2. 我们也可以说我们想要在没有字符串开头的whatspace上拆分。

  1. We can try to create regex which will match these whatspaces which have any character before them like a| b| c|.
  2. We can also say that we want to split on whatspaces that do not have start of string before them.

要创建此类正则表达式,我们需要 look-around 机制。

To create such regexes we will need look-around mechanisms.



    • 说空Stirng只需使用

    • 要说某些东西之前还需要其他东西,我们可以使用正面观察(?< =。)

    • To say empty Stirng just use ""
    • To say that something needs to have something else before it we can use positive-look-behind (?<=.).

如果我们将前两个品脱合并:(?< =。)我们将获得(?< =。)+这简直就是(?< =。)所以abc.split((?< =。))应该仅在这些以任何字符开头的空字符串上进行拆分(以点表示的正则表达式)。

If we will combine previous two pints: "(?<=.)" and "" we will get "(?<=.)"+"" which is simply "(?<=.)" so "abc".split("(?<=.)") should split only on these empty strings which are preceded by any character (in regex represented by dot .).

要说某些东西不能停留在字符串的开头,我们可以使用负面观察(?<!...) ^ 表示字符串的开头。所以(?<!^)表示条件在它之前没有字符串的开头。这就是为什么(?<!^)无法匹配此空格

To say that something can't stay at start of the string we can use negative-look-behind (?<!...) and ^ which represents start of the string. So (?<!^) represents condition "has no beginning of string before it". That is why "(?<!^) cant match this white space

 ↓  
"|a|b|c|"


因为它之前有字符串的开头。

since it has start of the string before it.

实际上还有一个特殊情况是你的问题的主要观点(?!^)这意味着负向前瞻。这个正则表达式描述了空字符串,后面没有字符串的开头。它有点不直观,因为之前我们假设字符串的开头(由 ^ 表示)放在这里

Actually there is also one special case which is main point of your question (?!^) which means negative-look-ahead. This regex describes empty string which do not have start of the string after it. It is kind of unintuitive, because previously we assumed that start of the string (represented by ^) is placed here

 ↓
"^|a|b|c|"

但现在它看起来像是这里:

but now it looks like it is here:

  ↓
"|^a|b|c|"

那是怎么回事?它是如何工作的?

正如我之前所说的,拆分空字符串很棘手。理解这一点你需要看一下没有标记空字符串的字符串,你会看到字符串的开头就是她e

So what is going on? How does it works?
As I told earlier splitting on empty strings is tricky. To understand this you need to take a look at string without marked empty strings and you will see that start of the string is here

 ↓
"^abc"

换句话说,正则表达式也考虑在第一个字符(在我们的例子中是a)之前的位置作为其开头,所以

In other words, regex also considers place right before first character (in our case "a") as its start, so

  ↓
"|^a|b|c|"

也有意义且有效,这就是为什么(?!^)能够看到这个空字符串

makes also sense and is valid, which is why (?!^) is able to see this empty string

 ↓
"|^a|b|c|"

在字符串开头之前,并且不接受它作为有效的分割位置。

as right before start of the string and will not accept it as valid place to split.

因为这对于那些不熟悉正则表达式的开发人员造成混淆,从Java 8开始我们不必使用技巧(?< =。)(?<!^)(?!^)避免在开头创建空字符串,因为如此问题所述

ANYWAY Since this was causing confusion for developers who ware not very familiar with regex, from Java 8 we don't have to use trick with (?<=.) or (?<!^) or (?!^) to avoid creating empty string at the beginning, because as described in this question

为什么在Java 8 split中有时会在结果数组的开头删除空字符串?

它会在生成的数组的开头自动删除空字符串,因为 split 中使用的长正则表达式代表零长度字符串(如空字符串),因此您现在可以使用abc.split()并得到结果 [a,b,c]

it automatically removes empty string at start of generated array as long regex used in split represents zero-length string (like empty string), so you now will be able to use "abc".split("") and get as result ["a", "b", "c"].

这篇关于是什么意思(?!^)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆