简体   繁体   中英

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 . 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.

How do I solve this problem?

  • Find: ^[^|]*\\|([^|]+).+$
  • Replace: $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:

^.*?\||\|.*$

Explanation

  • ^.*?\\| Match from start of string non greedy until the first pipe
  • | Or
  • \\|.*$ Match from last pipe until end of string

Regex demo

Or you might also use a negated character class [^|]* without the need of capturing groups:

^[^|]*\||\|[^|]*$

Regex demo

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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