Perl regex to extract floating number

前端 未结 3 983
情歌与酒
情歌与酒 2021-01-22 17:05

I need to modify someone\'s Perl script, and I am not familiar with Perl at all.

There is a scalar variable $var, whose value is a floating point number pos

3条回答
  •  遇见更好的自我
    2021-01-22 17:54

    Following your update, the expression you need is:

    ^\d+(?:\.\d+)?
    
    • ^\d+ Match digits at start of string.
    • (?: Start of non capturing group.
    • \.\d+ Match a literal ., followed by digits.
    • )? Close non capturing group making it optional.

    Check the expression here.

    A Perl example:

    $var = "123.456.789";
    print "old $var\n";
    $var =~ /(^\d+(?:\.\d+)?)/;
    print "new $1\n";
    

    Prints:

    old 123.456.789
    new 123.456
    

提交回复
热议问题