我正在尝试确定以下模式匹配条件允许我输入的内容:
\s*([\w\.-]+)\s*=\s*('[^']*'|"[^"]*"|[^\s]+)
从我试图破译(通过查看我所理解的正则表达式)似乎说我可以从任何字符序列开始然后我必须有一个括号后跟字母数字,然后是另一个序列后跟括号,一个初始单引号,没有用支撑关闭反斜杠???
对不起,如果我把这个搞得一团糟。任何帮助表示赞赏。
此致 巴勃罗
答案 0 :(得分:2)
方括号是字符类,而parens用于分组。我不确定你的“括号”是什么意思。
这基本上匹配name = value对,其中name由一个或多个“word”,点或连字符组成,并且值是单引号字符或双引号字符串,或者是一堆非空白字符。单引号字符不能包含单引号,双引号字符串可能不包含双引号(无论是什么语法,都可以说是小错误)。自从最后一个选项(“一堆非空白字符”)可以匹配以单引号或双引号开头的内容时,也存在一些歧义。
此外,等号或开头可能会出现零个或多个空格(即\s*
位)。
答案 1 :(得分:1)
它正在寻找基本上
的文本字符串<identifier> = <value>
标识符由字母,数字,' - '和'。'组成。
value 可以是单引号字符串,双引号字符串或任何其他字符序列(只要它不包含空格)。
所以它会匹配看起来像这样的行:
foo = 1234
bar-bar= "a double-quoted string"
bar.foo-bar ='a single quoted string'
.baz =stackoverflow.com this part is ignored
有些注意事项:
答案 2 :(得分:0)
\s*([\w\.-]+)\s*=\s*('[^']*'|"[^"]*"|[^\s]+)
Options: case insensitive
Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below and capture its match into backreference number 1 «([\w\.-]+)»
Match a single character present in the list below «[\w\.-]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
A word character (letters, digits, etc.) «\w»
A . character «\.»
The character “-” «-»
Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “=” literally «=»
Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below and capture its match into backreference number 2 «('[^']*'|"[^"]*"|[^\s]+)»
Match either the regular expression below (attempting the next alternative only if this one fails) «'[^']*'»
Match the character “'” literally «'»
Match any character that is NOT a “'” «[^']*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “'” literally «'»
Or match regular expression number 2 below (attempting the next alternative only if this one fails) «"[^"]*"»
Match the character “"” literally «"»
Match any character that is NOT a “"” «[^"]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “"” literally «"»
Or match regular expression number 3 below (the entire group fails if this one fails to match) «[^\s]+»
Match a single character that is a “non-whitespace character” «[^\s]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Created with RegexBuddy
答案 3 :(得分:0)
让我们分开\s*([\w\.-]+)\s*=\s*('[^']*'|\"[^\"]*\"|[^\s]+)
:
\s*([\w\.-]+)\s*
:
\s*
表示0个或更多空格字符 ('[^']*'|\"[^\"]*\"|[^\s]+)
:
基本上,你可以在尝试理解表达式时忽略\s*
,他们只是处理删除间距。
答案 4 :(得分:0)
是的,你完全搞糊涂了。 :P首先,正则表达式中没有括号;这个词通常指的是大括号:{}
。该正则表达式只包含方括号和括号(也称为圆括号),并且它们都是正则表达式元字符 - 它们并不意味着字面上匹配这些字符。大多数其他角色也是如此。
您可能会发现this site有用。所有正则表达式的非常好的教程和参考站点。