Regex to extract only text after string and before space

前端 未结 3 1312
南方客
南方客 2020-12-28 16:28

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         


        
相关标签:
3条回答
  • 2020-12-28 17:04

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

     ^BookTitle:([^\s]+)  
    

    Without multi-line option, this:

     (?:^|\n)BookTitle:([^\s]+)
    
    0 讨论(0)
  • 2020-12-28 17:15

    you can have positive lookbehind in your pattern.

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

    For more info: Lookahead and Lookbehind Zero-Width Assertions

    0 讨论(0)
  • 2020-12-28 17:17

    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.

    0 讨论(0)
提交回复
热议问题