简体   繁体   English

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

[英]Getting text between special characters using Regex

I'm trying to get the words between special character '|' 我正在尝试让特殊字符“ |”之间的单词 which are in format [az]+@[0-9]+ . 格式为[az]+@[0-9]+

Sample text - 示范文本 -

||ABC@123|abc@123456||||||ABcD@12||

Expected output - 预期产量-

ABC@123, abc@123456, ABcD@12

Regex i'm using 我正在使用的正则表达式

(?i)\\|[a-z]+@[0-9]+\\|

When I used this regex, the output i'm getting is |ABC@123| 当我使用此正则表达式时,我得到的输出是|ABC@123|

What mistake I'm doing ? 我在做什么错? Can somebody help me with this please ? 有人可以帮我吗?

You need to use Lookaround that matches but don't include it the match. 您需要使用相匹配的环顾四周 ,但不要将其包含在匹配中。

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

Here is regex101 online demo 这是regex101在线演示

Sample code: 样例代码:

String pattern = "(?i)(?<=\\||^)[a-z]+@[0-9]+(?=\\||$)";
String str = "|ABC@123|abc@123456|ABcD@12";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(str);
while (m.find()) {
    System.out.println(m.group());
}

output: 输出:

ABC@123
abc@123456
ABcD@12

Lookahead and lookbehind , collectively called lookaround , are zero-length assertions. Lookaheadlookbehind ,统称lookaround ,是零长度的断言。 The difference is that lookaround actually matches characters, but then gives up the match, returning only the result: match or no match. 区别在于,环顾四周实际上是匹配字符,但随后放弃了匹配,仅返回结果:匹配或不匹配。 That is why they are called "assertions". 这就是为什么它们被称为“断言”的原因。

Read more... 阅读更多...

Pattern explanation: 模式说明:

  (?<=                     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

You should not put the | 你不应该把| in your pattern, otherwise it will be matched. 按照您的模式,否则它将被匹配。 Use the lookaraound operators as in the other solution, or just match ( demo ): 与其他解决方案一样,使用lookaraound运算符,或者仅匹配( demo ):

[a-z]+@\d+

You should also consider splitting the string on | 您还应该考虑在|上拆分字符串。 as showed here . 这里所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM