开发者

What's wrong with this Perl regular expression?

开发者 https://www.devze.com 2022-12-26 17:26 出处:网络
I have 开发者_如何学编程a lines like this NF419andZNF773 (e=10^-92,). ZNF571 (e=2 10^-14,) What\'s the regex for extracting the results above so that it gives

I have 开发者_如何学编程a lines like this

NF419andZNF773 (e=10^-92,). 
ZNF571 (e=2 10^-14,)

What's the regex for extracting the results above so that it gives

NF419andZNF773 - 10^-92
ZNF571 - 2 10^-14

I tried this but fail.

$line =~ /(\w+)\s\(e=\s(.*),\)/;
print "$1 - $2\n";


You're close, the ending of your regex is failing since it expects space before the exponent. try this:

$line =~ / (\w+) \s+ \( e= ([^,]+) /x;


Actually you could do this all in regex, try

$line =~ s/\(\s*e\s*=\s*([^,]+),\)/-$1/

The regex matches the (e=num^exponent,) portion of your string and while doing that it captures the num^exponent (in $1) and then replaces the entire match with $1.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号