Python正则表达式搜索字母数字字符和正斜杠

2024-04-23 22:49:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我的尝试:

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

例如,如果word匹配所有条件,则应返回匹配

以下是一些例子:

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

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

谢谢


Tags: rereturn字母数字条件字符单词例子
2条回答

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

这应该与您的所有示例相匹配:

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

问题[^\/]匹配与正斜杠不同的任何字符\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)))

相关问题 更多 >