Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to extract only text after string and before space

Tags:

I want to match text after given string. In this case, the text for lines starting with "BookTitle" but before first space:

BookTitle:HarryPotter JK Rowling BookTitle:HungerGames Suzanne Collins Author:StephenieMeyer BookTitle:Twilight 

Desired output is:

HarryPotter HungerGames 

I tried: "^BookTitle(.*)" but it's giving me matches where BookTitle: is in middle of line, and also all the stuff after white space. Anyone help?

like image 503
user1899415 Avatar asked Sep 10 '13 01:09

user1899415


1 Answers

you can have positive lookbehind in your pattern.

 (?<=BookTitle:).*?(?=\s) 

For more info: Lookahead and Lookbehind Zero-Width Assertions

like image 72
John Woo Avatar answered Sep 20 '22 17:09

John Woo