Marco Lau
Marco Lau

Reputation: 11

Regex with varying whitespaces

I'm trying to write Regex to find lines in a files where commands are not using absolute paths. Unfortunately, there are spacing issues with these lines.

// PATHS
pathmunge /absolute/path
  pathmunge   ~not/absolute/path
 pathmunge           /absolute/path


// matches

  pathmunge   ~not/absolute/path

My expression matches the lines where there are spaces at the beginning and before pathmunge string, but it doesn't find the lines with variable-length whitespace but without a "/" as the next non-whitespace character.

So far, I have:

^(?=\s+pathmunge)\s+(?!\/).*$

Any help is appreciated.

Upvotes: 1

Views: 118

Answers (2)

Toto
Toto

Reputation: 91430

Remove the lookahead and make the space possessive:

^\s*pathmunge\s++(?!\/).*$

Demo & explanation

Upvotes: 1

dawg
dawg

Reputation: 103874

For a regex, I would do:

^(?:[[:blank:]]*pathmunge[[:blank:]]+([^\s\/].*$))

Demo

The key element is the [^\s\/] which matches a single character other than a horizontal space or a \n or / start of any absolute path.

Upvotes: 1

Related Questions