Home > Mobile >  Regular Expressions to find a string included between two symbols while EXCLUDING the preceding / en
Regular Expressions to find a string included between two symbols while EXCLUDING the preceding / en

Time:09-10

I need to extract from a string a set of characters (TEXT1 / TEXT2 / TEXT3) which are included between two delimiters, without returning the delimiters themselves, nor the preceding or ending spaces.

My strings look like this:

  1. TEXT1 | characters
  2. digits.digits - TEXT2 | characters
  3. characters - digits digits-digits-digits - TEXT3 | characters
  4. characters - digits digits/digits/digits - TEXT4 | characters

Is it possible to write a **single **regex that would work to extract TEXT1 / TEXT2 / TEXT3 / TEXT4 for all strings above?

If not, how could I extract for each case?

I tried:

  1. (.*?)(?=|) - but I don't know how to leave out the space after TEXT1.
  2. (?<=-)(.*?)(?=|) - but I don't know how to leave out the space before and after TEXT2

CodePudding user response:

[^\d-]*(\w )\s?(?=\|)

[^\d-]* match all but digits and "-"

(\w ) match words

\s? match whitespace or not

(?=\|) lookahead "|"

you can also tinker it here

CodePudding user response:

You can use (?:-\h )?(\w )\h \|.

Demo

  • Related