简体   繁体   English

如何使用Perl正则表达式从等式中提取具有非零系数的变量?

[英]How can I extract variables with nonzero coefficients from an equation using a Perl regex?

I wish to grab the non-zero coefficients of a particular number arrangement. 我希望获得特定数字排列的非零系数。 I have the set of numbers below and I wish to strictly extract with regex the non-zero coefficient ( x2 , x3 , x5 ) from 我有下面的数字集,我希望用正则表达式严格提取非零系数( x2x3x5

x1 *0.000000+ x2 *-0.100000+ x3 *0.850000+ x4 *0.000000+ x5 *0.000056+ x6 *0.000000 

I tried doing this 我试过这样做

(\*[-+]?[0]*\.?[0]+)

but am not really sure as to declare that it shouldn't consider the "*0.000000" 但我不确定是否应该宣布它不应该考虑"*0.000000"

Match x-digit not followed by space-star-zero-dot-zeroes-endOfWord: 匹配x-digit后面没有 space-star-zero-dot-zeroes-endOfWord:

x\d+(?! \*0\.0+\b)

See live demo . 查看现场演示

I would recommend using the fact that you have full-blown programming language at your disposal to simplify the regex to capture all coefficients, and then use grep to eliminate the zeros: 我建议您使用这样一个事实,即您可以使用完整的编程语言来简化正则表达式以捕获所有系数,然后使用grep消除零:

#!/usr/bin/env perl

use strict;
use warnings;

my $eqn = "x1 *0.000000+ x2 *-0.100000+ x3 *0.850000+ x4 *0.000000+ x5 *0.000056+ x6 *0.000000 ";

my %eqn = ($eqn =~ /(x[0-9]+) \s [*] (-?[0-9.]+) [+]/gx);

print "$_\n" for grep $eqn{$_} != 0, keys %eqn;

Output (order will not be consistent unless you sort variable names): 输出(除非您对变量名称进行排序,否则顺序将不一致):

x3 
x5 
x2

This might look like too much extra work, but having the equation parsed into a hash which maps variables to coefficients may actually save you work down the road depending on the exact nature of what you are doing. 这可能看起来太多额外的工作,但将方程式解析为将变量映射到系数的哈希可能实际上可以根据您正在做的事情的确切性质来保存您的工作。 I am assuming you are doing some kind of model selection based on coefficient magnitudes. 我假设您正在根据系数幅度进行某种模型选择。

Do: 做:

x\d+(?!\s*\*0\.0*(?:[+\s]|$))
  • x\\d+ gets x followed by one or more digits x\\d+得到x后跟一个或多个数字
  • The zero width negative lookahead pattern (?!\\s*\\*0\\.0*(?:[+\\s]|$)) ensures that the match is not followed by zero co-efficients 零宽度负前瞻模式(?!\\s*\\*0\\.0*(?:[+\\s]|$))确保匹配后不是零效率

Demo 演示


More robust, avoiding false positives in the match: 更强大,避免在比赛中出现误报:

x\d+(?!\s*\*0\.0*(?:[+\s]|$))(?=\s*\*-?\d+(?:\.\d*)?(?:[+\s]|$))

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM