正则表达式提取数字,空格和&来自字符串的分数

时间:2017-09-26 13:32:00

标签: regex

我阅读和阅读困难写正则表达式,我想从以下字符串中提取数字,空格和分数:

给定字符串: 5 1/2

预期结果: root@ubuntu:/home/gimenes/Downloads/qt-everywhere-opensource-src-4.7.0# make configure /home/gimenes/Downloads/qt-everywhere-opensource-src-4.7.0/bin/qmake -spec mkspecs/linux-g++-64 -o Makefile projects.pro make: /home/gimenes/Downloads/qt-everywhere-opensource-src-4.7.0/bin/qmake: Command not found Makefile:1063: recipe for target 'Makefile' failed make: *** [Makefile] Error 127 root@ubuntu:/home/gimenes/Downloads/qt-everywhere-opensource-src-4.7.0# make /home/gimenes/Downloads/qt-everywhere-opensource-src-4.7.0/bin/qmake -spec mkspecs/linux-g++-64 -o Makefile projects.pro make: /home/gimenes/Downloads/qt-everywhere-opensource-src-4.7.0/bin/qmake: Command not found Makefile:1063: recipe for target 'Makefile' failed make: *** [Makefile] Error 127

我应该使用什么正则表达式?

1 个答案:

答案 0 :(得分:1)

试试这个正则表达式:

\b\d+\s+\d+\/\d+\b

Click for Demo

<强>解释

  • \b - Word-boundary
  • \d+ - 匹配1+位数
  • \s+ - 匹配1 +空格
  • \d+\/\d+ - 匹配1位数后跟/后跟1位数字
  • \b - 字边界