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 Rowli
With the 'multi-line' regex option use something like this:
^BookTitle:([^\s]+)
Without multi-line option, this:
(?:^|\n)BookTitle:([^\s]+)
you can have positive lookbehind
in your pattern.
(?<=BookTitle:).*?(?=\s)
For more info: Lookahead and Lookbehind Zero-Width Assertions
What language is this?
And provide some code, please; with the ^
anchor you should definitely only be matching on string that begin with BookTitle, so something else is wrong.
If you can guarantee that all whitespace is stripped from the titles, as in your examples, then ^BookTitle:(\S+)
should work in many languages.
Explanation:
^
requires the match to start at the beginning of the string, as you know.
\s
- *lower*case means: match on white*s*pace (space, tab, etc.)
\S
- *upper*case means the inverse: match on anything BUT whitespace.
\w
is another possibility: match on *w*ord character (alphanumeric plus underscore) - but that will fail you if, for example, there's an apostrophe in the title.
+
, as you know, is a quantifier meaning "at least one of".
Hope that helps.