Let’s say I have these three lines:
the quick brown fox
the brown fox
the quick brown quick fox
Can regex be used to crop out everything in each line except for the word quick?
The end result would look like this:
quick
quickquick
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The specifics depend on the language you are using, but here are a few common approaches for doing this with regex (code samples in Python):
Find all matches of your target string, and then combine each match into a single string:
Construct a regex to match everything except your target string, and then replace each match with an empty string (this is usually much more difficult than the other alternatives listed):
Use capturing groups to match everything up until an occurrence of a target string, and then replace with just the target string:
If your string has multiple lines as in your example, you can either split the strings on line breaks first or adapt the solutions to keep line breaks, for example: