如何将这些perl正则表达式转换为python

时间:2015-05-14 19:43:53

标签: python regex perl

我正在尝试将perl脚本转换为python,并且我挂起了正则表达式语句。

Perl脚本:

open NICKNAME, $NICKNAMEFILE or die "$NICKNAMEFILE: $!";
<NICKNAME> =~ /.+[.].+[.](.+)/;
my $project = $1;
$struct{'project'} = $project;
close NICKNAME;

open KEYFILE, $KEYHASHFILE or die "$KEYHASHFILE: $!";
<KEY> =~ /^([-\w\d]+)$/;
my $key = $1;
$struct{'key'} = $key;
close KEYFILE;

的Python:

nickname = open(NICKNAMEFILE, "r")
project = re.search(#something)
struct['project'] = project
nickname.close()

keyfile = open(KEYHASHFILE, "r")
key = re.search(#something)
struct['key'] = key
keyfile.close()

1 个答案:

答案 0 :(得分:0)

Python和Perl的正则表达式语法相同。你的问题更多&#34;这个正则表达式搜索的是什么。&#34;

引用Python正则表达式库,我可以推导出以下内容(https://docs.python.org/2/library/re.html):

r'.+[.].+[.](.+)'

正在搜索表格中的某些内容

(one or more character).(one or more character).(one or more character)

这一个:

r'^([-\w\d]+)$'

正在搜索表格中的某些内容

a string starting with any number of letters A-Z/a-z or number 0-9 and no special characters