Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用正则表达式查找文本并仅获取其中的一部分_Javascript_Regex - Fatal编程技术网

Javascript 使用正则表达式查找文本并仅获取其中的一部分

Javascript 使用正则表达式查找文本并仅获取其中的一部分,javascript,regex,Javascript,Regex,我需要找到一个带有正则表达式的文本,比如说“andres”,但它不能介于[] 例如,如果文本为: s = 'andres [andres andres] andres [andres] andresX andres' 我应该得到第一个、第四个、第六个和最后一个,其他人至少有一个[],因此它们不匹配 我试过这个: "[^\[]andres[^\]]" 更好的例子 但它不起作用。试试这个: 测试字符串为: $string = 'andres [an1dres an1dres] andres [

我需要找到一个带有正则表达式的文本,比如说“andres”,但它不能介于
[]

例如,如果文本为:

s = 'andres [andres andres] andres [andres] andresX andres' 
我应该得到第一个、第四个、第六个和最后一个,其他人至少有一个
[]
,因此它们不匹配

我试过这个:

"[^\[]andres[^\]]"
更好的例子

但它不起作用。

试试这个: 测试字符串为:

$string = 'andres [an1dres an1dres] andres [an1dres] andresX andres' ;

$patern = '/\\[.*?\\]| /';
试试这个: 测试字符串为:

$string = 'andres [an1dres an1dres] andres [an1dres] andresX andres' ;

$patern = '/\\[.*?\\]| /';
尝试:

  • 您可能希望也可能不希望设置不区分大小写选项
  • 您可能需要也可能不需要设置^$match at line break选项
解释

查找后跟

  • 不是右括号的一系列字符,然后按
  • 开口括号或字符串的末端
RegexBuddy的解释:

andres not between [ ... ]

andres(?=[^[\]]+(?:\[|$))

Options: Case sensitive; ^$ match at line breaks

Match the character string “andres” literally (case sensitive) «andres»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[^[\]]+(?:\[|$))»
   Match any single character NOT present in the list below «[^[\]]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      The literal character “[” «[»
      The literal character “]” «\]»
   Match the regular expression below «(?:\[|$)»
      Match this alternative (attempting the next alternative only if this one fails) «\[»
         Match the character “[” literally «\[»
      Or match this alternative (the entire group fails if this one fails to match) «$»
         Assert position at the end of a line (at the end of the string or before a line break character) (line feed, line feed, line separator, paragraph separator) «$»

Created with RegexBuddy
尝试:

  • 您可能希望也可能不希望设置不区分大小写选项
  • 您可能需要也可能不需要设置^$match at line break选项
解释

查找后跟

  • 不是右括号的一系列字符,然后按
  • 开口括号或字符串的末端
RegexBuddy的解释:

andres not between [ ... ]

andres(?=[^[\]]+(?:\[|$))

Options: Case sensitive; ^$ match at line breaks

Match the character string “andres” literally (case sensitive) «andres»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[^[\]]+(?:\[|$))»
   Match any single character NOT present in the list below «[^[\]]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      The literal character “[” «[»
      The literal character “]” «\]»
   Match the regular expression below «(?:\[|$)»
      Match this alternative (attempting the next alternative only if this one fails) «\[»
         Match the character “[” literally «\[»
      Or match this alternative (the entire group fails if this one fails to match) «$»
         Assert position at the end of a line (at the end of the string or before a line break character) (line feed, line feed, line separator, paragraph separator) «$»

Created with RegexBuddy

在正则表达式中,有一种很有用的模式可以做这类事情:

exclusion_context1|exclusion_context2|...|(stuff_you_want)
您可以指定任意数量的排除上下文,并在最后捕获捕获组中确实需要的内容。我可以进一步解释,但实际上,我将对上述模式进行深入讨论

那么:

\[.*?\]|(andres)

我们的排除上下文惰性地匹配括号内的任何内容,否则我们将捕获该上下文之外的所有
andres

因为我刚刚注意到您需要匹配的位置,所以在python中可能看起来像这样:

for m in re.finditer(r'\[.*?\]|(andres)', s):
    if m.group(1):
        print('{}: {}'.format(m.start(),m.group()))

0: andres
23: andres
39: andres
47: andres

在正则表达式中,有一种很有用的模式可以做这类事情:

exclusion_context1|exclusion_context2|...|(stuff_you_want)
您可以指定任意数量的排除上下文,并在最后捕获捕获组中确实需要的内容。我可以进一步解释,但实际上,我将对上述模式进行深入讨论

那么:

\[.*?\]|(andres)

我们的排除上下文惰性地匹配括号内的任何内容,否则我们将捕获该上下文之外的所有
andres

因为我刚刚注意到您需要匹配的位置,所以在python中可能看起来像这样:

for m in re.finditer(r'\[.*?\]|(andres)', s):
    if m.group(1):
        print('{}: {}'.format(m.start(),m.group()))

0: andres
23: andres
39: andres
47: andres

您可以使用以下选项:

\w+(?![^\[]*\])


您可以使用以下功能:

\w+(?![^\[]*\])



也许
“([^\[]andres)|(andres[^\]])|([^\[]andres.[^\]])”
?它会考虑<代码> [安德烈斯< /代码>,<代码>安德烈斯> <代码>或>代码> […安德烈斯…] /代码>。为什么你要写自己的名字7次,然后尝试与正则表达式匹配,你不应该知道你的名字是什么吗?JavaScript还是Python?你使用了两个标签。我也对如何应用这两个标签感兴趣。这段代码的作用是什么?在字符串上传递2次就可以了,首先过滤掉[]之间和[]之间的任何内容,然后使用一个简单的正则表达式来查找文本maybe
([^\[]andres)|(andres[^\]])|([^\[]andres.[^\]])“
?它会考虑<代码> [安德烈斯< /代码>,<代码>安德烈斯> <代码>或>代码> […安德烈斯…] /代码>。为什么你要写自己的名字7次,然后尝试与正则表达式匹配,你不应该知道你的名字是什么吗?JavaScript还是Python?你使用了两个标签。我也对如何应用这两个标签感兴趣。这段代码的作用是什么?在字符串上传递2次就可以了,首先过滤掉介于[]和[]之间的任何内容,然后使用简单的正则表达式查找文本我需要指定一个特定的字符串,在示例中我使用了我的名字,但它可以是我指定特定字符串所需的内容,在这个例子中,我使用了我的名字,但它可以是第39行的小组应该写的“andres”而不是“andresX”哦。嗯,只是。。将
andres
放入正则表达式中,以代替先前的捕获组。已编辑。如果添加“andres”,它将只返回(0、23和47),我还想添加带有空格的文本,如“andres arrieche”忘记我的最后一条评论,我原以为你想添加“\bandres\b”,但现在你的答案是对的,我明白你的意思了。第39行的组应该写“andres”而不是“andresX”哦。嗯,只是。。将
andres
放入正则表达式中,以代替先前的捕获组。已编辑。如果添加“andres”,它将只返回(0、23和47),我还想添加带有空格的文本,如“andres arrieche”忘记我的最后一条评论,我原以为你想添加“\bandres\b”,但现在你的答案是对的啊,我明白你当时的意思了。