Zhi J Teoh
Zhi J Teoh

Reputation: 61

Find a line with leading and/or trailing space(s)

Create a regular expression that will find a line with leading and/or trailing space(s).

Guys, I have no idea how to start. Anyone care to lighten me up?

Upvotes: 2

Views: 1639

Answers (3)

zx81
zx81

Reputation: 41838

To see if such lines exist, you can use this simple regex (see online demo):

^ | $

To actually match the lines, use this (see online demo):

^(?: .*$|.* $)

To match a the spaces, use this (see online demo):

^ +| +$

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

You can use this:

/^ .*|.+ $/m

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

Your regex would be like this to find the lines which has leading and trailing spaces,

^ +.* +$

Upvotes: 0

Related Questions