English 中文(简体)
下面的正方形会做什么?
原标题:What does the following regex do?
  • 时间:2012-05-25 14:26:11
  •  标签:
  • regex

下面的正方形是做什么的?

[w+-.]+

以上正则、 w+- + 和 w+- + 和 w+- + 的输出相同,但我不明白为什么?

测试字符串

sadkfj-dslk.sdjklf!sdljf

你能解释给我听吗?

最佳回答

同样,因为反斜线是多余的;当 - 不表示有效的字符范围( 即

给定的regex 查找这些字符中的一个或多个字符 : w (一个字字符)、 (加号)、 - (dash) 或 . (周期)。 由于在 - 之前的反斜是不必要的,无论是否包含在内。

问题回答

在方括号内,连字符有特殊含义(“范围”),因此,字面连字符应作为 - 逃避。然而,视你特定的regex 引擎而定,你可能会与未逃脱的语法脱节,因为在此特定情况下没有模糊之处。

(例如, Perl 将接受两个版本并产生预期结果, 但使用 < code> 使用警告; 它将抱怨缺失的反斜线 。 )

这符合单词字符 [0- 9a- z- A- ] (w 匹配单词字符), 或 + 字符, 或连字符( 跳出 - ), 或 完全停止, 所有这一切都重复不止一次 。

这意味着它会匹配含有普通字母、下划线、加号、加号、连字符或完整停止的字符串, 只要没有空格。 简言之, 对我来说, 这似乎是从 URL 识别字符串以查找数据库条目的函数 。

没有区别, 因为逃离- 没有必要, 因为没有字符匹配“ + to ”, 所以逃逸或逃逸, 没有什么区别 。





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签