请解释这个 Perl 正则表达式 [英] Please explain this Perl regular expression

查看:65
本文介绍了请解释这个 Perl 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    $rowfetch =~ s/['-]//g; #All chars inside the [ ] will be filtered out.
    $rowfetch =~ m/(\w+), ?(.)/;
    printf $fh lc($2.$1);

昨天我得到了构建这个正则表达式的帮助,但我并不完全理解它.

它需要一个像 Parisi、Kenneth 这样的名字并打印出 kparisi

已知:
s/= 替代
m/= 匹配


我尝试搜索其余部分,但找不到任何真正有助于解释它的内容.

我也不明白=~ 应该如何评估为真或假,但在这种情况下,它正在修改字符串.

I got help building this regular expression yesterday, but I don't fully understand it.

It takes a name like Parisi, Kenneth and prints out kparisi

Knowns:
s/ = substitute
m/ = match


I tried searching for the rest but couldn't find anything that really helped explain it.

I also didn't understand how the =~ is supposed to evaluate to either true or false, yet in this situation, it is modifying the string.

推荐答案

我保留其中一个 备忘单就在这种场合固定在我的立方体墙上.Google for 正则表达式备忘单以查找其他人.

I keep one of these cheat sheets pinned on my cube wall for just such occasions. Google for regular expression cheat sheet to find others.

补充你已经知道的:

  g -- search globally throughout the string
  + -- match at least one, but as many as possible
  ? -- match 0 or 1
  . -- match any character
 () -- group these together
  , -- a plain comma, no special meaning
 [] -- match any character inside the brackets
 \w -- match any word character

神奇在于分组——匹配表达式使用组并将它们放入变量 $1 和 $2 中.在这种情况下,$1 匹配逗号前的单词,$2 匹配逗号后空格后的第一个字符.

The magic is in the grouping -- the match expression uses the groups and puts them into variables $1 and $2. In this case $1 matches the word before the comma and $2 matches the first character following the whitespace after the comma.

这篇关于请解释这个 Perl 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆