2

It might sound a weird request and I totally understand that this is not the purpose of regex. I am just looking for a possible workaround or a feature that is less used / documented (if any)

The scenario is I can provide a regex as an admin for configuring an option where a file / web service response is parsed for matches and same is returned to end user; (Application and other details don't seem to be important but if you are interested please let me know.)

Now is it possible that I specify a string in the pattern that is not considered from matching but is returned with the match. like

[String :] [regex pattern]

and now the regex engine ignores the [string] and for matches returns

[String : match1] [String : match2] [String : match3] ...

1
  • If the engine supports lookbehinds, that's usually what you'd go for, otherwise, using capture groups and using only the captured part(s). Commented Mar 3, 2014 at 9:54

1 Answer 1

1

If you want to have a suffix added to all the lines matched with a regex then you can try something like the following,

The following command adds the string "STRING" to all the matched lines,

grep -w "Your_Pattern" Your_FILE | sed -e 's/^/STRING/' 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.