从字符串中获取文本的特定部分 [英] Grabbing specific sections of text from a string
本文介绍了从字符串中获取文本的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,我需要从中提取两个部分 - 它们各不相同,因为它是一个 PHP 语言文件.希望有人能帮忙,字符串是:
I've got a string that I need to grab two section out - they vary as it's a PHP language file. Hoping someone can help, the string is:
$_LANG['FIELD1'] = "FIELD2";
我需要获取 FIELD1 和 FIELD2
I need to grab FIELD1 and FIELD2
推荐答案
应该这样做:
# space seperated
$ sed -n "s/.*_LANG['([^']*)'] = .(w*).*/1 2/p" file
FIELD1 FIELD2
# newline seperated
$ sed -n "s/.*_LANG['([^']*)'] = .(w*).*/1
2/p" file
FIELD1
FIELD2
或者将 grep
与 positive lookbehind 结合使用::>
Or using grep
with positive lookbehind:
$ grep -Po "(?<=_LANG[')[^']*" file
FIELD1
$ grep -Po '(?<=_LANG[.FIELD1.] = ")[^"]*' file
FIELD2
这篇关于从字符串中获取文本的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文