[英]RegEx for removing everything before and after a delimiter
我正在尝试删除两个之前和之后的所有内容|
使用正则表达式的定界符。
一个例子是:
EM|CX-001|Test Campaign Name
并抓住除CX-001
以外的所有东西。 我不能使用子字符串,因为管道前后的字符数可能会发生变化。
我尝试使用正则表达式(?<=\\|)(.*?)(?=\\-)
,但是虽然这选择了CX-001
,但我需要选择除此以外的所有内容。
我该如何解决这个问题?
^[^|]*\\|([^|]+).+$
$1
您可以尝试以下正则表达式:
(^[^|]*\|)|(\|[^|]*$)
String input = "EM|CX-001|Test Campaign Name";
System.out.println(
input.replaceAll("(^[^|]*\\|)|(\\|[^|]*$)", "")
); // prints "CX-001"
正则表达式的说明:
NODE EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[^|]* any character except: '|' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
\| '|'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
\| '|'
--------------------------------------------------------------------------------
[^|]* any character except: '|' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of \2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.