grep regex lookahead or start of string (or lookbehind or end of string) -


i want match string may contain type of character before match, or match may begin @ beginning of string (same end of string).

for minimal example, consider text n.b., i'd match either @ beginning of line , end of line or between 2 non-word characters, or combination. easiest way use word boundaries (\bn\.b\.\b), doesn't match; similar cases happen other desired matches non-word characters in them.

i'm using (^|[^\w])n\.b\.([^\w]|$), works satisfactorily, match non-word characters (such dashes) appear before , after word, if available. i'm doing in grep, while pipe output sed, i'm using grep's --color option, disabled when piping command (for obvious reasons).

edit: \k option (i.e. (\k^|[^\w])n\.b\.(\k[^\w]|$) seems work, discard color on match within output. while could, again, invoke auxiliary tools, i'd love if there quick , simple solution.

edit: have misunderstood \k operator; removes text match preceding use. no wonder failing color output.

if you're using grep, must using -p option, or lookarounds , \k throw errors. means have negative lookarounds @ disposal. here's simpler version of regex:

(?<!\w)n\.b\.(?!\w) 

also, aware (?<=...) , (?<!...) lookbehinds, , (?=...) , (?!...) lookaheads. wording of title suggests may have gotten mixed up, common beginner's mistake.