'regex pattern for table structure string

see this

i am struggle with match the pattern with empty column. my regex pattern look like

^(.*?\$).*?(\w\:\\.*?)\s(.*?)$.

this pattern match the not empty column in a row, but i want to match the empty column in a row.



Solution 1:[1]

Brief

It's difficult to replicate your issue without the exact input (and not just a picture of it), so I hope I have the correct input below. In any case, the issue you're having is that you haven't made the middle column optional. I've recreated your regex to better perform and also allow changed \s to \h (horizontal whitespace characters). You haven't specified a regex engine, so I'm assuming anything works. If \h is unsupported by your engine you can use [ \t] instead.


Code

See regex in use here

^([^$]+\$)\h+([a-z]:\\\S*)?\h+(.*)$

Results

Input

IPC$                                            Remote IPC
ADMIN$          C:\Windows                      Remote Admin
D$              D:\                             Default share
C$              C:\                             Default share

Output

  • Match 1: IPC$ Remote IPC
    • Group 1: IPC$
    • Group 3: Remote IPC
  • Match 2: ADMIN$ C:\Windows Remote Admin
    • Group 1: ADMIN$
    • Group 2: C:\Windows
    • Group 3: Remote Admin
  • Match 3: D$ D:\ Default share
    • Group 1: D$
    • Group 2: D:\
    • Group 3: Default share
  • Match 4: C$ C:\ Default share
    • Group 1: C$
    • Group 2: C:\
    • Group 3: Default share

Explanation

  • ^ Assert position at the start of the line
  • ([^$]+\$) Capture the following into capture group 1
    • [^$]+ Match any character except $ one or more times
    • \$ Match $ literally
  • \h+ Match one or more horizontal whitespace characters
  • ([a-z]:\\\S*)? Optionally capture the following into capture group 2
    • [a-z] Match any character in this range (any lowercase letter - with i flag it also matches uppercase variants)
    • :\\ Match :\ literally
    • \S* Match any number of non-whitespace characters
  • \h+ Match one or more horizontal whitespace characters
  • (.*) Capture any character any number of times into capture group 3
  • $ Assert position at the end of the line

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1