0

This question is a little confusing. I want to match everything in between the things I am searching for including the string I am using to search. For example, I have this list:

  1. This is a sentence
  2. This is a fake
  3. This can be a sentence

I want it to match the entire line. For this case I want it to match This and sentence, so it returns This is a sentence and This can be a sentence. I have tried something like this: Regex Match all characters between two strings. That does not work because it does not take the original keywords. It would only take the matched string BETWEEN the keywords.

How can this be accomplished?

Thanks!

Community
  • 1
  • 1
ayao1337
  • 135
  • 1
  • 6

2 Answers2

0

Seems like the expression "This.*sentence" will give you everything. That is, it matches This, then all characters, then sentence. If either This or sentence is missing, then it won't match.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

^This.*sentence$

I think this should work.

If you want to extract, then you use the parenthesis.

The ^ notes the beginning of the line and the $ marks the end. ^This matches This at the start of the line. sentence$ matches sentence as the last word at the end of the line.

Anthony
  • 3,492
  • 1
  • 14
  • 9
  • $ echo "This is a sentence" | grep ^This.*sentence$ This is a sentence $ echo "This is a sentenceaa" | grep ^This.*sentence$ $ echo "This is a fake" | grep ^This.*sentence$ $ echo "This can be a sentence" | grep ^This.*sentence$ This can be a sentence – Anthony Nov 23 '14 at 00:10
  • What does that mean? I've found that removing the "$" does the job, but only for the first line. – ayao1337 Nov 23 '14 at 00:13
  • http://pastebin.com/eEGp070P The formatting in my response is messed up so it looks like one long line. Apparently, the response could only be edited for 5 minutes and the time expired. Look at the output of the pastebin link. The regex looks fine to me. – Anthony Nov 23 '14 at 00:15
  • Anthony: Comments don't let you do any fancy formatting. You could edit your answer to add the information from your comment. It would be good information to show that your regex does indeed work. – Jim Mischel Nov 23 '14 at 00:24
  • I don't understand how this works. It does not match in regexr when I test it. – ayao1337 Nov 23 '14 at 00:30