Perl 中的斜杠和散列以及元字符

发布于 2024-10-18 01:11:43 字数 796 浏览 6 评论 0原文

感谢大家之前的帮助!我有一个关于 Perl 中的 RegExp 的查询

我的问题是..

我知道,匹配时你可以写 m// 或 // 或 ## (如果你使用这个,则必须包括 m 或 s)。引起我困惑的是一本关于我所拥有的转义字符的书本示例。我相信大多数人都会转义很多字符,这是程序运行时不会丢失元字符的一种可靠方式,即:\@ 当在电子邮件地址中查找匹配 @ 时。

这是我的问题,我知道这个脚本的作用:

$date= "15/12/99"
$date=~ s#(\d+)/(\d+)/(\d+)#$1/$2/$3#; << why are no forward slashes escaped??
print($date);

然而我后面的例子显示它被重写,因为(我也理解并且它们被转义)

$date =~ s/()(\d+)\/(\d+)\/(d+)/$2\/$1\/$3; <<<<which is escaping the forward slashes.

我知道斜杠或散列是程序员的偏好及其用途。我不明白的是为什么第二个例子逃避了斜线,而第一个例子却没有——我已经尝试过了,它们是双向的。没有用散列转义斜杠?更令人困惑的是,看看我之前也有的另一本书示例,再次使用哈希,它们也转义了 @ 符号。

if ($address =~ m#\@#) { print("这是一个电子邮件地址"); } 或类似的东西

那么,您如何从不使用散列或斜杠的内容中转义呢?我知道你必须转义元字符才能匹配它们,但我很困惑。

Thanks for the previous assistance everyone!. I have a query regarding RegExp in Perl

My issue is..

I know, when matching you can write m// or // or ## (must include m or s if you use this). What is causing me the confusion is a book example on escaping characters I have. I believe most people escape lots of characters, as a sure fire way of the program working without missing a metacharacter something ie: \@ when looking to match @ say in an email address.

Here's my issue and I know what this script does:

$date= "15/12/99"
$date=~ s#(\d+)/(\d+)/(\d+)#$1/$2/$3#; << why are no forward slashes escaped??
print($date);

Yet the later example I have, shows it rewritten, as (which i also understand and they're escaped)

$date =~ s/()(\d+)\/(\d+)\/(d+)/$2\/$1\/$3; <<<<which is escaping the forward slashes.

I know the slashes or hashes are programmer preference and their use. What I don't understand is why the second example, escapes the slashes, yet the first doesn't - I have tried and they work both ways. No escaping slashes with hashes? What's even MORE confusing is, looking at yet another book example I also have earlier to this one, using hashes again, they too escape the @ symbol.

if ($address =~ m#\@#) { print("That's an email address"); } or something similar

So what do you escape from what you don't using hashes or slashes? I know you have to escape metacharacters to match them but I'm confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

养猫人 2024-10-25 01:11:43

当您构建正则表达式时,您可以定义一个字符作为正则表达式的分隔符,即执行 //##
如果您需要在正则表达式中使用该字符,则需要对其进行转义,以便正则表达式引擎不会将其视为正则表达式的结尾。

如果您在正斜杠 / 之间构建正则表达式,则需要转义正则表达式中包含的正斜杠,因此在第二个示例中进行转义。

当然,同样的规则适用于用作正则表达式分隔符的任何字符,而不仅仅是正斜杠。

When you build a regexp, you define a character as a delimiter for your regexp i.e. doing // or ##.
If you need to use that character inside your regexp, you will need to escape it so that the regexp engine does not see it as the end of the regexp.

If you build your regexp between forward slashes /, you will need to escape the forward slashes contained in your regexp, hence the escaping in your second example.

Of course, the same rule apply with any character you use as a regexp delimiter, not just forward slashes.

北城挽邺 2024-10-25 01:11:43

正斜杠本身不是元字符 - 只是在第二个示例中将它们用作表达式分隔符使它们变得“特殊”。

替换表达式的格式为:

s<expression separator char><expression to look for><expression separator char><expression to replace with><expression separator char>

在第一个示例中,使用哈希作为 =~ 后的第一个字符,使该字符成为表达式分隔符,因此正斜杠并不特殊,不需要任何转义。
在第二个示例中,表达式分隔符确实是正斜杠,因此必须在表达式本身内对其进行转义。

The forward slashes are not meta characters in themselves - only the use of them in the second example as expression separators makes them "special".

The format of a substitute expression is:

s<expression separator char><expression to look for><expression separator char><expression to replace with><expression separator char>

In the first example, using a hash as the first character after the =~ s, makes that character the expression separator, so forward slash is not special and does not require any escaping.
in the second example, the expression separator is indeed the forward slash, so it must be escaped within the expressions themselves.

王权女流氓 2024-10-25 01:11:43

正则表达式匹配运算符允许将自定义非空白字符定义为分隔符。

在第一个示例中,“#”用作分隔符。因此,在这个正则表达式中,您不需要转义“/”,因为它没有特殊含义。在第二个正则表达式中,分隔符字符未更改。因此使用默认的“/”。现在你必须转义模式中的所有“/”。否则解析器会感到困惑。 :)

The regex match-operator allows to define a custom non-whitespace-character as seperator.

In your first example the '#' is used as seperator. So in this regex you don't need to escape the '/' because it hase no special meaning. In the second regex, the seperator char isn't changed. So the default '/' is used. Now you have to escape all '/' in your pattern. Otherwise the parser is confused. :)

挽清梦 2024-10-25 01:11:43

如果不使用斜杠,建议的做法是使用花括号和 /x 修饰符。

$date=~ s{ (\d+) \/ (\d+) \/ (\d+) }{$1/$2/$3}x;

即使非字母数字不是元字符,转义也是一个标准。请参阅perldoc -f quotemeta

If you are not use slashes, the recommend practice is to use the curly braces and the /x modifier.

$date=~ s{ (\d+) \/ (\d+) \/ (\d+) }{$1/$2/$3}x;

Escaping the non-alphanumerics is also a standard even if they are not meta-characters. See perldoc -f quotemeta.

空袭的梦i 2024-10-25 01:11:43

关于使用 s 运算符转义正斜杠,这个问题还有另一个深度。
以我的例子来说,捕获就成了问题。

$image_name =~ s/((http:\/\/.+\/)\/)/$2/g;

为此,必须捕获添加第二个正斜杠的拼写错误。
此外,尝试仅使用两个斜杠是行不通的。第一个斜杠必须由多个字符开头。

更改“http://world.com/Photos//space_shots/out_of_this_world.jpg”
至:“http://world.com/Photos/space_shots/out_of_this_world.jpg”

There is another depth to this question about escaping forward slashes with the s operator.
With my example the capturing becomes the problem.

$image_name =~ s/((http:\/\/.+\/)\/)/$2/g;

For this to work the typo with the addition of a second forward slash, had to be captured.
Also, trying to work with just the two slashes did not work. The first slash has to be led by more than one character.

Changing "http://world.com/Photos//space_shots/out_of_this_world.jpg"
To: "http://world.com/Photos/space_shots/out_of_this_world.jpg"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文