Regex retrieves only text after line and before space

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

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

Required Conclusion:

 HarryPotter HungerGames 

I tried: "^BookTitle(.*)" , But he gave me matches where BookTitle: is in the middle of the line, as well as all the materials after the space. Anyone help?

+8
regex
source share
3 answers

you may have a positive lookbehind in your template.

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

For More Information: Lookahead and Lookbehind Zero Width Statements

+16
source share

What language is it in? And indicate the code, please; with an anchor ^ you should definitely only match the line starting with BookTitle, so something else is wrong. If you can guarantee that all spaces are removed from the titles, as in your examples, then ^BookTitle:(\S+) should work in many languages.
Explanation:
^ requires that the match starts at the beginning of the line, as you know.
\s - * lower * case means: match on white * s * pace (space, tab, etc.)
\s - * upper * case means the opposite: a match on nothing, but in a space.
\w is another possibility: matching the character * w * ord (alphanumeric plus underscore) - but this will not help you if, for example, there is an apostrophe in the name.
+ , as you know, is a quantifier meaning "at least one of."
Hope this helps.

+5
source share

With the 'multi-line' regex option, use something like this:

  ^BookTitle:([^\s]+) 

Without a multi-line option, this is:

  (?:^|\n)BookTitle:([^\s]+) 
+3
source share

All Articles