Get numbers from string with regex

前端 未结 5 1989
Happy的楠姐
Happy的楠姐 2020-12-07 00:27

I am trying to write a regex to get the numbers from strings like these ones:

javascript:ShowPage(\'6009\',null,null,null,null,null,null,null)
javascript:Bloc         


        
相关标签:
5条回答
  • 2020-12-07 01:05

    Assuming:

    • you want to capture the digits
    • there's only one set of digits per line

    Try this:

    /(\d+)/
    

    then $1 (Perl) or $matches[1] (PHP) or whatever your poison of choice is, should contain the digits.

    0 讨论(0)
  • 2020-12-07 01:09

    Try this:

    (\d+)
    

    What language are you using to parse these strings?

    If you let me know I can help you with the code you would need to use this regular expression.

    0 讨论(0)
  • 2020-12-07 01:18

    just match numbers: \d+

    0 讨论(0)
  • 2020-12-07 01:29
    // PHP
    
    $string = 'ssss 12.2';
    $pattern = '/\D*(\d+)(.|,)?(\d+)?\D*/';
    
    $replacement = '$1.$3';
    
    $res = (float)preg_replace($pattern, $replacement, $string);
    
    // output 12.2
    
    0 讨论(0)
  • 2020-12-07 01:32

    Integer or float:

    /\d+((.|,)\d+)?/
    
    0 讨论(0)
提交回复
热议问题