Perl 单行匹配所有出现的正则表达式 [英] Perl one-liner to match all occurrences of regex

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

问题描述

对于多行与此类似的文本:

For multiple lines of text similar to this:

"views_panes","gw_hero_small_site_placement-panel_pane_1",1,"a:0:{}","a:10:{s:14:\"override_title\";i:1;s:19:\"override_title_text\";s:0:\"\";s:9:\"view_mode\";s:11:\"all_purpose\";s:11:\"image_style\";s:7:\"default\";s:13:\"style_options\";a:2:{s:10:\"show_image\";i:0;s:9:\"show_date\";i:0;}s:18:\"gw_display_options\";s:22:\"gw_all_purpose_sidebar\";s:13:\"show_readmore\";a:1:{s:18:\"show_readmore_link\";i:0;}s:14:\"readmore_title\";s:9:\"Read more\";s:13:\"readmore_link\";s:0:\"\";s:7:\"exposed\";a:1:{s:23:\"field_hero_sub_type_tid\";s:3:\"547\";}}","a:0:{}","a:1:{s:8:\"settings\";N;}","a:0:{}","a:0:{}",0,"s:0:\"\";"

我希望匹配 (s:)(\d{1,}:)\"(string)\"; 的所有实例,以获得这样的结果:

I am looking to match all instances of (s:)(\d{1,}:)\"(string)\"; to get something like this:

s:14:override_title
s:18:show_readmore_link
s:3:547

这行有或没有 /g 只打印第一个实例:

This line with or without /g prints only the first instances:

perl -nle 'print  "$1 $2 $3"  if /(s:)(\d{1,}:)\\"(.*?)\\";/g' tmp.txt

s:14:override_title

我想我可以尝试将其放入 perl 脚本中,将所有匹配项放入一个数组中,但我希望使用单行(-:我错过了什么?

I suppose I can try to put this in a perl script putting all matches into an array, but am hoping to do this using a one-liner (-: What am I missing?

Mac OS X 10.7.5,perl 5.12.3.

Mac OS X 10.7.5, perl 5.12.3.

推荐答案

看来你只有一行,所以试试:

It's seem you have only line, so have a try with:

perl -nle 'print  "$1 $2 $3"  while(/.*?(s:)(\d{1,}:)\\"(.*?)\\";/g)' tmp.txt

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

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