Remove empty spaces and period

前端 未结 3 1269
悲&欢浪女
悲&欢浪女 2021-01-25 08:03

I cannot get this regex to work:

\"4.  182 ex\" (number, period, 2 blank spaces, 3 numbers, blank space, 2 characters\"

The regex syntax should

相关标签:
3条回答
  • 2021-01-25 08:08

    For it to match exactly what you said, use:

    (\d)\.\s\s(\d{3})\s\w\w
    

    You'll get it in two groups, first digit and second digit group.

    RegEx101 exmple

    Regards.

    0 讨论(0)
  • 2021-01-25 08:20

    Just remove all characters that are not digit:

    Perl:

    $string =~  s/\D+//g;
    

    or php:

    $string = preg_replace('/\D+/', '', $string);
    

    According to your updated question, you could do:

    $string =~ s/^Magic(\d+)\.  (\d{3})\b.*$/$1$2/
    

    or, with php:

    $string = preg_replace('/^Magic(\d+)\.  (\d{3})\b.*$/', '$1$2', $string);
    
    0 讨论(0)
  • 2021-01-25 08:27
    ^([\d]+)\.[\s]+([\d]+)[\s]..
    

    Tested with perl:

    > echo "4. 182 ex" | perl -lne 'print $1,$2 if(/^([\d]+)\.[\s]+([\d]+)[\s]../)'
    4182
    
    0 讨论(0)
提交回复
热议问题