正则表达式匹配问题

时间:2019-03-02 14:37:43

标签: regex

我正在尝试编写一个正则表达式以匹配以下规则:

  1. 一个单词仅由字母,数字,撇号,连字符和下划线组成

  2. 以字母或撇号开头,后跟字母

  3. 不包含两个或多个撇号,下划线或连字符的序列
  4. 以字母,数字或撇号结尾,后跟字母s或撇号,然后是s

到目前为止,我已经构建了一些正则表达式:

对于规则2,我已经建立

 override func viewDidLoad() {
    super.viewDidLoad()
    self.addMiddleImage()
}

对于规则3,我已经建立

^[']?[a-zA-Z][a-zA-Z0-9]+

但是对于测试字符串(?!.*[-_'][-_'])(?=[a-z])[a-zA-Z0-9]* ,它匹配abc def''ghi而不是ghi

对于规则4,我已经建立

abc

但是对于测试字符串.*[a-zA-Z0-9](?:'s)?(?:s')?$,它不匹配任何内容,但应匹配test's abc'

我正在寻找有关规则3和4的一些建议,以改善我的正则表达式,以便它们起作用

1 个答案:

答案 0 :(得分:1)

(?:^|\s)\K(?!'')['a-z](?:['_-]?[a-z0-9])+['_-]?(?:(?<!')'s|s'|[a-z])(?=\s|$)

说明:

(?:^|\s)            # non capture group, beginning of line OR space
\K                  # forget all we've seen until this position
(?!'')              # negative lookahead, not two apos.
['a-z]              # apos. or letter
(?:                 # start non capture group
    ['_-]?          # apos, dash or underscore, optional
    [a-z0-9]        # a letter or digit
)+                  # group may appear 1 or more times
['_-]?              # apos, dash or underscore, optional
(?:                 # start non capture group
    (?<!')          # negative lookbehind, make sure we haven't apos before
    's              # apos and s
  |                 # OR
    s'              # s and apos
  |                 # OR
    [a-z]           # a letter
)                   # end group
(?=\s|$)            # lookahead, make sure we have a space or end of line after

Demo