Home > Net >  Regex to select the 2nd whitespace after a matched character
Regex to select the 2nd whitespace after a matched character

Time:12-06

I am trying to match the middle whitespace out of 3 whitespaces in a row.

I want to only match the middle whitespace but only if it precedes a |. The actual text I have to match from is this:

 --- --- ---
|   |   |   |
 --- --- ---

I can match 2 whitespaces in a row that precede | with (?<!\|)\s{2} (the xs represent the matches) but I only want to match the x in the middle

 --- --- ---
| xx| xx| xx|
 --- --- ---

CodePudding user response:

What if you try to split the string and use white space as separator, get the substring in the middle then match it to the regex pattern?

CodePudding user response:

You can use grouping to get these specific spaces:
The bolded section relates to the spaces that you want:

(|\s{3}|\s)(\s{2})(|\s{3}|)

Code:

import re

text = """
 --- --- ---
|   |   |   |
 --- --- ---
 """
 
pattern = re.compile(r"(\|\s{3}\|\s)(\s{2})(\|\s{3}\|)")

match = pattern.search(text)
print(match.group(2))

Here is a regex101 : demo

  • group(0) matches the entire pattern from start to end. | | | |
  • group(1) matches the pattern before the whitespace of interest
  • group(2) matches the whitespace of interest
  • group(3) matches the pattern after the whitespace of interest
  • Related