首页 > 解决方案 > 正则表达式从一个字符开始获取两个字符之间的所有内容

问题描述

标签: regexregex-lookarounds

解决方案


In

re'\gfby  by  A »;ŸÎ                 \     l o l l i .
p o p - c o l \ n t u s e r . d a t   dŸŸœ~®ã€º &¹UÚÂdŸŸœ~®ã€º
&¹UÚ    eŸŸœ~®ã€º &¹UÚÂrmtmÖpïJÔ

this

(?:(?!  )[^\\.-])+\.(?:(?!  )[^\\.-])+

finds

 l o l l i .
p o p 

and

 n t u s e r . d a t

Breakdown:

(?:           # begin non-capturing group
  (?!  )      #   look-ahead: any position not followed by two spaces
  [^\\.-]     #   any character except '\', '.', and `-`
)+            # end group, repeat at least once
\.            # a '.'
(?:           # \
  (?!  )      #  |-- exactly the same as above
  [^\\.-]     #  |
)+            # /
  • Use * instead of + if you want to allow matches that start or end with a dot.
  • Single spaces are valid parts of the match, so they might occur at the start or end with this (in the above sample, they do). Trim the result if needed.
  • Since the matching is done by a negated character class, we get the side conditions "...or the start/end of the string" and "include newlines" for free.

推荐阅读