Create a regular expression in linuxunix that will find a li

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

Solution

Explanation :

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

^ asserts position at start of a line

Non-capturing group

(?: .*$|.* $)

1st Alternative

.*$

matches the character literally (case sensitive)

.*

matches any character (except for line terminators)

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

$ asserts position at the end of a line

2nd Alternative

.* $

.*

matches any character (except for line terminators)

* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)

matches the character literally (case sensitive)

$ asserts position at the end of a line

Global pattern flags

g modifier: global. All matches (don\'t return after first match)

m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Create a regular expression in linux/unix that will find a line with leading and/or trailing space(s).SolutionExplanation : ^(?: .*$|.* $) ^ asserts position at

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site