【问题标题】:Python regex search for alphanumeric characters and forward slashPython正则表达式搜索字母数字字符和正斜杠
【发布时间】:2021-02-09 18:47:21
【问题描述】:

我正在尝试查找某个单词是否符合以下条件:

  • 仅使用字母数字(和下划线)字符,还可以选择使用正斜杠。
  • 是 1 或 2 行
  • 每行有 1-4 个字符

我的尝试:

pattern = r"\w{1,4}(\n[^\/]\w{1,4})?"
return bool(re.fullmatch(pattern, word))

例如如果word 匹配所有条件,它应该返回一个匹配项。

这里有一些例子:

  • “眼睛”
  • “眼睛\n1/2”
  • “软\nTISS”
  • “BLAD\n2”

字母数字部分有效,但正斜杠 [^\/] 加法无效。有什么建议吗?

谢谢!

【问题讨论】:

  • [^\/] 是一个 negated 字符类,你需要一个简单的 / 来代替。您能否解释一下斜线可能出现在哪些上下文中?它可以每行出现一次吗?还是只能在字符串只有一行长的时候?
  • 如果你包含几个这样的词的例子,它可能会非常有用。
  • 在原帖中添加了一些示例
  • 如果您只想在字符串中的任何位置允许单个/,您应该考虑r"^(?!(?:[^/]*/){2})[\w/]{1,4}(?:\n[\w/]{1,4})?"

标签: python regex


【解决方案1】:

问题[^\/] 匹配任何不同于正斜杠的字符。 \w 不匹配斜线。

使用

pattern = r"(?=(?:[^/]*/)?[^/]*$)[\w/]{1,4}(\n[\w/]{1,4})?"
return bool(re.fullmatch(pattern, word))

proof

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      [^/]*                    any character except: '/' (0 or more
                               times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
      /                        '/'
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
    [^/]*                    any character except: '/' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [\w/]{1,4}               any character of: word characters (a-z, A-
                           Z, 0-9, _), '/' (between 1 and 4 times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1 (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \n                       '\n' (newline)
--------------------------------------------------------------------------------
    [\w/]{1,4}               any character of: word characters (a-z,
                             A-Z, 0-9, _), '/' (between 1 and 4 times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of \1 (NOTE: because you are 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

Python code

import re
strings = ["EYE", "EYE\n1/2", "SOFT\nTISS", "BLAD\n2"]
for s in strings:
    print(bool(re.fullmatch(r'(?=(?:[^/]*/)?[^/]*$)[\w/]{1,4}(\n[\w/]{1,4})?', s)))

【讨论】:

    【解决方案2】:

    如果要匹配/,只需使用r'/'

    这应该匹配你所有的例子:

    r'\w{1,4}(\n[\w/]{1,4})?'
    

    【讨论】:

    • 在这种情况下,^$ 是多余的,因为 OP 使用需要完整字符串匹配的 re.fullmatch
    • 啊哈,没注意到!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2015-07-14
    • 1970-01-01
    • 2021-08-22
    • 2014-05-24
    • 2020-03-27
    相关资源
    最近更新 更多