Issue
I have a string in a file say:
abc
or
def abc
I want to match abc
such that it can either appear at the beginning of the line or after a blank.
I can do it with matching multiple patterns, separating with |
like:
/^abc|[[:blank:]]+abc/
But is there some way to include ^
i.e. beginning of the string something like character class.
^
at the beginning of character class would mean negation and other positions would mean just match the caret ('^') character.
Also, in my original case, the string is not just abc
, instead it is a string matched with a a complex regex, so I prefer it to be as clean as possible. I am matching this regex inside an awk
script. Although I still include bash
tag because system()
function can still be used within it. Speed doesn't matter much to me in my case, but clarity does.
Solution
Use a capturing group:
(^|[[:blank:]]+)abc
It will match the beginning of the string or a run of blanks. You can drop +
if you don't need all blanks preceding abc
in your match but the last.
Answered By - oguz ismail Answer Checked By - Clifford M. (WPSolving Volunteer)