Regex to find last occurrence of pattern in a string

前端 未结 4 1273
一个人的身影
一个人的身影 2021-01-14 00:02

My string being of the form:

\"as.asd.sd fdsfs. dfsd  d.sdfsd. sdfsdf sd   .COM\"

I only want to match against the last segment of whitespa

4条回答
  •  [愿得一人]
    2021-01-14 00:53

    "as.asd.sd ffindMyLastOccurrencedsfs. dfindMyLastOccurrencefsd  d.sdfsd. sdfsdf sd   ..COM"
    
    .*(?=((?<=\S)\s+)).*
    
    replaced by `>\1<`
    
    >   <
    

    As a more generalized example

    "as.asd.sd ffindMyLastOccurrencedsfs. dfindMyLastOccurrencefsd  d.sdfsd. sdfsdf sd   ..COM"
    
    .*(?=(findMyLastOccurrence|(?<=\S)\s+|(?<=[^\.])\.+)).*
    
    replaced by `>\1<`
    
    >..<
    

    Explanation:

    Part 1 .*

    • is greedy and finds everything as long as the needles are found. Thus, it also captures all needle occurrences until the very last needle.

    edit to add:

    • in case we are interested in the first hit, we can prevent the greediness by writing .*?

    Part 2 (?=(findMyLastOccurrence|(?<=\S)\s+|(?<=[^\.])\.+|(?<=**Not**NeedlePart)NeedlePart+))

    • defines the 'break' condition for the greedy 'find-all'. It consists of several parts:
      (?=(needles))
      • positive lookahead: ensure that previously found everything is followed by the needles findMyLastOccurrence|(?<=\S)\s+|(?<=[^\.])\.+)|(?<=**Not**NeedlePart)NeedlePart+
      • several needles for which we are looking. Needles are patterns themselves.
      • In case we look for a collection of whitespaces, dots or other needleparts, the pattern we are looking for is actually: anything which is not a needlepart, followed by one or more needleparts (thus needlepart is +). See the example for whitespaces \s negated with \S, actual dot . negated with [^.]

    Part 3 .*

    • as we aren't interested in the remainder, we capture it and dont use it any further. We could capture it with parenthesis and use it as another group, but that's out of scope here

提交回复
热议问题