[英]RegEx for removing everything before and after a delimiter
I am trying to remove everything before and after two |
我正在尝试删除两个之前和之后的所有内容|
delimiters using regex. 使用正则表达式的定界符。
An example being: 一个例子是:
EM|CX-001|Test Campaign Name
and grabbing everything except CX-001
. 并抓住除CX-001
以外的所有东西。 I cannot use a substring as the number of characters before and after the pipes may change. 我不能使用子字符串,因为管道前后的字符数可能会发生变化。
I tried using the regex (?<=\\|)(.*?)(?=\\-)
, but while this selects CX-001
, I need to select everything else but this. 我尝试使用正则表达式(?<=\\|)(.*?)(?=\\-)
,但是虽然这选择了CX-001
,但我需要选择除此以外的所有内容。
How do I solve this problem? 我该如何解决这个问题?
^[^|]*\\|([^|]+).+$
查找: ^[^|]*\\|([^|]+).+$
$1
更换: $1
If you have only 2 pipes in you string, you could either match upon the first pipe or match from the last one until the end of the string: 如果字符串中只有2个管道,则可以匹配第一个管道,也可以从最后一个管道匹配到字符串末尾:
^.*?\||\|.*$
Explanation 说明
^.*?\\|
Match from start of string non greedy until the first pipe 从字符串非贪心开始到第一个管道匹配 |
Or 要么 \\|.*$
Match from last pipe until end of string \\|.*$
从最后一个管道匹配到字符串结尾 Or you might also use a negated character class [^|]*
without the need of capturing groups: 或者,您也可以使用否定的字符类[^|]*
而不需要捕获组:
^[^|]*\||\|[^|]*$
Note 注意
In your pattern (?<=\\|)(.*?)(?=\\-)
I think you meant that the last positive lookahead should be (?=\\|)
instead of the -
if you want to select between 2 pipes. 在您的模式中(?<=\\|)(.*?)(?=\\-)
我想您的意思是,如果要在2个管道之间进行选择,则最后一个正向前行应为(?=\\|)
而不是-
。
You can try the following regular expression: 您可以尝试以下正则表达式:
(^[^|]*\|)|(\|[^|]*$)
String input = "EM|CX-001|Test Campaign Name";
System.out.println(
input.replaceAll("(^[^|]*\\|)|(\\|[^|]*$)", "")
); // prints "CX-001"
Explanation of the regular expression: 正则表达式的说明:
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.