正则表达式工具

输出结果:
1
2  let str = '123456';
3  let result = str.match(/\d*/);
4  if (result) {
5    console.log(result[0]); // group 0
6    console.log(result[1]); 
7  }
8  
9  
1
2  import java.util.regex.Matcher; 
3  import java.util.regex.Pattern;   
4  
5  public class TestReplace {
6
7	public static void main(String[] args) {
8		Pattern p = Pattern.compile("\\d*");
9		Matcher m = p.matcher("123456");
10        while (m.find()) {
11			System.out.println("group()" + m.group());//等价于group(0)
12			System.out.println("group(1)" + m.group(1));
13			System.out.println("group(2)" + m.group(2));
14		}
15	}
16  }
17  
1 
2import re
3
4s = '123456';
5result = re.match("\d*", s)
6print(result)
7print(result.group()) # 等价于group(0)
8print(result.group(1))
9
10
1
2  string regex = @"\d*";
3  string input = "123456";
4  Match match = Regex.Match(input, regex);
5  for (int i = 0; i < match.Groups.Count; i++)
6  {
7      Console.WriteLine("match.Groups[" + i + "]=" +match.Groups[i].Value);
8  }
9
10  
1
2#include<iostream>
3#include<regex>
4#include<string>
5
6using namespace std;
7
8int main() 
9{    
10    string matchstr("123456");
11    smatch results;
12    try {
13        // regex r("[[:alnum:]]+\.(cpp|cxx|cc)$", regex::icase)
14        regex re("\\d*");
15        while (regex_match(matchstr,results, re))
16        {
17            for (auto s : results) 
18                cout << s << endl;
19            matchstr = results.suffix().str();
20        }
21    }
22    catch (regex_error e)
23    {
24        cout << e.what() << "
25code:" << e.code() << endl;
26    }
27    return 0;
28}
1
2  echo "123456" | grep -P "\d*";
3  #注意Mac的grep 不支持-P, 不支持 (?<=...) or *? or (?=...
4 
5  echo "123456" | sed -n '/\d*/p'
6
7  echo "123456" | awk '/\d*/{print $0}'
8  
参考

如果想了解正则表达式概念:正则表达式入门指南

想更深入了解正则表达式请看:深入了解正则表达式

实践正则表达式,可以参考:各种正则表达式的例子

各种语言实现的正则表达式,可以参考:各种编程语言的正则表达式

代码交流 2021