正则表达式用于唯一用户数

时间:2015-10-14 15:00:50

标签: regex

我正在尝试创建一个正则表达式来检查唯一身份用户的数量。 在这种情况下,1个字符串中的3个不同用户表示它有效。 我们说我们有以下字符串

  

实验室\西蒙;实验室\利芬;实验室\添; \实验室\戴维;实验室\利芬

它包含每个用户(实验室)的域名及其名字。 每个用户都分开;

目标是在字符串中包含3个唯一用户。 在这种情况下,字符串有效,因为我们有以下唯一用户

  西蒙,谎言,蒂姆,戴维=有效

如果我们拿这个字符串

  

实验室\西蒙;实验室\利芬;实验室\西蒙

它无效,因为我们只有2个唯一身份用户

  

simon,lieven = invalid

到目前为止,我只提出以下正则表达式,但我不知道如何继续

/(lab)\\(?:[a-zA-Z]*)/g

你能帮我解决这个正则表达式吗?

如果您需要更多信息,请告诉我,如果不清楚的话。

6 个答案:

答案 0 :(得分:0)

你所追求的是无法通过自己的正则表达来实现的。正则表达式用于解析信息而不是处理。

你没有特别的模式,这是正则表达式所擅长的。您需要按;拆分并使用数据结构(如集合)来存储字符串值。

答案 1 :(得分:0)

这就是你想要的:

1)使用正则表达式:

.vimrc

2)不使用正则表达式:

import re

s = r'lab\simon;lab\lieven;lab\tim;\lab\davy;lab\lieven'
pattern = re.compile(r'lab\\([A-z]{1,})')
user = re.findall(pattern, s)

if len(user) == len(set(user)) and len(user) >= 3:
    print('Valid')
else:
    print('Invalid')

答案 2 :(得分:0)

为了成功匹配,我们需要至少 3套lab\user,即:

(?:\\?lab\\[\w]+(?:;|$)){3}

您没有指定引擎,但使用python可以使用:

import re

if re.search(r"(?:\\?lab\\[\w]+(?:;|$)){3}", string):
    # Successful match
else:
    # Match attempt failed

Regex Demo

正则表达式解释

(?:\\?lab\\[\w]+(?:;|$)){3}

Match the regular expression «(?:\\?lab\\[\w]+(?:;|$)){3}»
   Exactly 3 times «{3}»
   Match the backslash character «\\?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match the character string “lab” literally «lab»
   Match the backslash character «\\»
   Match a single character that is a “word character” «[\w]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match the regular expression below «(?:;|$)»
      Match this alternative «;»
         Match the character “;” literally «;»
      Or match this alternative «$»
         Assert position at the end of a line «$»

答案 3 :(得分:0)

这是一种解决问题的初学者友好方式:

  1. 你应该.split()每个"实验室" section并将结果声明为数组变量,如splitted_string。
  2. 声明第二个空数组以保存每个唯一名称,例如unique_names。
  3. 使用for循环遍历splitted_string数组。检查唯一名称:如果它不在unique_names数组中,请将名称添加到unique_names。
  4. 查找unique_names数组的长度,看它是否等于3.如果是,则打印出来。如果没有,则打印失败消息。
  5. 你似乎是一个对字符串操作相对较新的实用人物。也许你会喜欢在初学者网站上进行字符串操作的实际背景阅读,如Automate The Boring Stuff With Python: https://automatetheboringstuff.com/chapter6/ 或者Codecademy等。

答案 4 :(得分:0)

这可以通过一个简单的正则表达式来完成 对每个用户名 slot 使用条件,以便所需的
获得三个名字。

请注意,由于三个插槽处于循环中,因此条件保证了 捕获组不会被覆盖(这将使下面提到的无效 断言测试(?! \1 | \2 | \3 )

有一个并发症。每个用户名使用相同的正则表达式[a-zA-Z]+ 为了适应这种情况,定义了一个函数来检查 以前没有匹配过。

这是使用增强引擎,美观上要求组为
在被引用之前定义。
解决方法是在定义组后定义底部的函数。

在PERL(以及其他一些引擎)中,不需要在前面定义组 在它被引用之前的时间,所以你可以取消功能
并把

(?! \1 | \2 | \3 )       # Cannot have seen this user
[a-zA-Z]+  

在顶部的捕获组中。

至少,这需要条件。

Formatted and tested:

 # (?:(?:.*?\blab\\(?:((?(1)(?!))(?&GetUser))|((?(2)(?!))(?&GetUser))|((?(3)(?!))(?&GetUser))))){3}(?(DEFINE)(?<GetUser>(?!\1|\2|\3)[a-zA-Z]+))

 # Look for 3 unique users
 (?:
      (?:
           .*? 
           \b lab \\    
           (?:
                (                   # (1), User 1
                     (?(1) (?!) )
                     (?&GetUser) 
                )
             |  (                   # (2), User 2
                     (?(2) (?!) )
                     (?&GetUser) 
                )
             |  (                   # (3), User 3
                     (?(3) (?!) )
                     (?&GetUser) 
                )
           )
      )
 ){3}

 (?(DEFINE)
      (?<GetUser>                   # (4)
           (?! \1 | \2 | \3 )       # Cannot have seen this user
           [a-zA-Z]+ 
      )
 )

答案 5 :(得分:0)

这项运动的另一个纯正的正则表达式答案。正如其他人所说,你可能不应该这样做

^([^;]+)(;\1)*;((?!\1)[^;]+)(;(\1|\3))*;((?!\1|\3)[^;]+)

说明:

^                 from the start of the string
([^;]+)           we catch everything that isn't a ';'.
                   that's our first user, and our first capturing group
(;\1)*            it could be repeated
;((?!\1)[^;]+)    but at some point, we want to capture everything that isn't either
                   our first user nor a ';'. That's our second user,
                   and our third capturing group
(;(\1|\3))*       both the first and second user can be repeated now
;((?!\1|\3)[^;]+) but at some point, we want to capture yada yada,
                   our third user and fifth capturing group