正则表达式匹配单词和撇号

Hum*_*art 12 regex python-3.x

更新:根据关于我的问题含糊不清的评论,我增加了问题的细节.

(术语:用语言来指代任何一系列字母数字字符.)

我正在寻找一个正则表达式来匹配以下,逐字:

  • 话.
  • 开头有一个撇号的单词.
  • 整个中间任意数量的非连续撇号的单词.
  • 最后有一个撇号的单词.

我想匹配以下内容,但不是逐字逐句,而是删除撇号:

  • 在开头和结尾处带有撇号的单词将与单词匹配,而不带撇号.所以'foo'会匹配foo.
  • 在中间具有多个连续撇号的单词将被解析为两个不同的单词:连续撇号之前的片段和连续撇号之后的片段.所以,foo''bar将匹配foobar.
  • 在开头或结尾处具有多个连续撇号的单词将与单词匹配,而没有撇号.因此,''foo将匹配foo''foo''foo.

示例 这些将逐字匹配:

  • 'bout
  • it's
  • persons'

但这些将被忽略:

  • '
  • ''

并且,因为'open',open将匹配.

mač*_*ček 21

试试这个:

(?=.*\w)^(\w|')+$

'bout     # pass
it's      # pass
persons'  # pass
'         # fail
''        # fail
Run Code Online (Sandbox Code Playgroud)

正则表达式解释

NODE      EXPLANATION
  (?=       look ahead to see if there is:
    .*        any character except \n (0 or more times
              (matching the most amount possible))
    \w        word characters (a-z, A-Z, 0-9, _)
  )         end of look-ahead
  ^         the beginning of the string
  (         group and capture to \1 (1 or more times
            (matching the most amount possible)):
    \w        word characters (a-z, A-Z, 0-9, _)
   |         OR
    '         '\''
  )+        end of \1 (NOTE: because you're using a
            quantifier on this capture, only the LAST
            repetition of the captured pattern will be
            stored in \1)
  $         before an optional \n, and the end of the
            string
Run Code Online (Sandbox Code Playgroud)


Whi*_*ind 4

/('\w+)|(\w+'\w+)|(\w+')|(\w+)/
Run Code Online (Sandbox Code Playgroud)
  • '\w+匹配 ' 后跟一个或多个字母字符,或者
  • \w+'\w+匹配一个或多个字母字符,后跟一个 ',后跟一个或多个字母字符,或者
  • \w+'匹配一个或多个字母字符,后跟 '
  • \w+匹配一个或多个字母字符