Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
Java 使用正则表达式获取特殊字符之间的文本_Java_Regex_String Matching - Fatal编程技术网

Java 使用正则表达式获取特殊字符之间的文本

Java 使用正则表达式获取特殊字符之间的文本,java,regex,string-matching,Java,Regex,String Matching,我正在尝试获取特殊字符“|”之间的单词,其格式为[a-z]+@[0-9]+ 样本文本- ||ABC@123|abc@123456||||||ABcD@12|| 预期产出- ABC@123, abc@123456, ABcD@12 我用的是正则表达式 (?i)\\|[a-z]+@[0-9]+\\| 当我使用这个正则表达式时,我得到的输出是|ABC@123| 我犯了什么错误?有人能帮我一下吗?你需要用那个火柴,但不要把它包括在火柴里 (?<=\||^)[a-z]+@[0-9]+(?=\|

我正在尝试获取特殊字符“|”之间的单词,其格式为
[a-z]+@[0-9]+

样本文本-

||ABC@123|abc@123456||||||ABcD@12||
预期产出-

ABC@123, abc@123456, ABcD@12
我用的是正则表达式

(?i)\\|[a-z]+@[0-9]+\\|
当我使用这个正则表达式时,我得到的输出是
|ABC@123|

我犯了什么错误?有人能帮我一下吗?

你需要用那个火柴,但不要把它包括在火柴里

(?<=\||^)[a-z]+@[0-9]+(?=\||$)

Lookahead
Lookahead
,统称为
Lookahead
,是零长度断言。区别在于lookaround实际上匹配字符,但随后放弃匹配,只返回结果:匹配或不匹配。这就是为什么它们被称为“断言”

模式说明:

  (?<=                     look behind to see if there is:
    \|                       '|'
   |                        OR
    ^                        the beginning of the line
  )                        end of look-behind

  [a-z]+                   any character of: 'a' to 'z' (1 or more times)
  @                        '@'
  [0-9]+                   any character of: '0' to '9' (1 or more times)

  (?=                      look ahead to see if there is:
    \|                       '|'
   |                        OR
    $                         the end of the line
  )                        end of look-ahead

(?您不应该将
|
放入您的模式中,否则它将被匹配。请像在其他解决方案中一样使用lookaround运算符,或者只匹配():


你也应该考虑在代码< > <代码>中分割字符串。

只使用<代码>分裂()/<代码>和“>”。it@assylias-我想了想。但后来我想,他也可以用
“[|]”
:P。所以,我没有提到
“\\\\”
ABC@123
abc@123456
ABcD@12
  (?<=                     look behind to see if there is:
    \|                       '|'
   |                        OR
    ^                        the beginning of the line
  )                        end of look-behind

  [a-z]+                   any character of: 'a' to 'z' (1 or more times)
  @                        '@'
  [0-9]+                   any character of: '0' to '9' (1 or more times)

  (?=                      look ahead to see if there is:
    \|                       '|'
   |                        OR
    $                         the end of the line
  )                        end of look-ahead
[a-z]+@\d+