课题
- 使用正则表达式匹配字符串
使用正则表达式 "\d{3}-(\d{4})-\d{2}" 匹配字符串 "123-4567-89"
返回匹配结果:’"123-4567-89" 以及 "4567"
- 使用正则表达式替换字符串(模式)
使用正则表达式 "(\d+)-(\d+)-(\d+)" 匹配字符串 "123-4567-89"
使用模式字符串 "$3-$1-$2" 替换匹配结果,返回结果 "89-123-4567"。
- 使用正则表达式替换字符串(回调)
使用正则表达式 "\d+" 匹配字符串 "123-4567-89"
将匹配结果即三个数字串全部翻转过来,返回结果 "321-7654-98"。
- 使用正则表达式分割字符串
使用正则表达式 "%(begin|next|end)%" 分割字符串"%begin%hello%next%world%end%"
返回正则表达式分隔符之间的两个字符串 "hello" 和 "world"。
Java
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s = "123-4567-89,987-6543-21";
Pattern r = Pattern.compile("\\d{3}-(\\d{4})-\\d{2}");
Matcher m = r.matcher(s);
for (int i = 0; m.find(); i++) {
if (i == 0)
System.out.println("Found matches:");
for (int j = 0; j <= m.groupCount(); j++)
System.out.printf("group %d,%d : %s\n", i, j, m.group(j));
}
System.out.println(s.replaceFirst("(\\d+)-(\\d+)-(\\d+)", "$3-$1-$2"));
// https://stackoverflow.com/questions/19737653/what-is-the-equivalent-of-regex-replace-with-function-evaluation-in-java-7
r = Pattern.compile("\\d+");
m = r.matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find())
m.appendReplacement(sb, new StringBuffer(m.group(0)).reverse().toString());
m.appendTail(sb);
System.out.println(sb.toString());
r = Pattern.compile("%(begin|next|end)%");
s = "%begin%hello%next%world%end%";
System.out.println(Arrays.asList(r.split(s)));
}
}
/*
Found matches:
group 0,0 : 123-4567-89
group 0,1 : 4567
group 1,0 : 987-6543-21
group 1,1 : 6543
89-123-4567,987-6543-21
321-7654-98,789-3456-12
[, hello, world]
*/
C#
using System;
using System.Text.RegularExpressions;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
var r = new Regex(@"\d{3}-(\d{4})-\d{2}");
var s = "123-4567-89,987-6543-21";
{
var m = r.Match(s);
if (m.Success)
Console.WriteLine("Found matches:");
}
foreach (var (m, i) in r.Matches(s).Cast<Match>().Select((m, i) => (m, i)))
foreach (var (g, j) in m.Groups.Cast<Group>().Select((g, j) => (g, j)))
Console.WriteLine($"group {i},{j} : {g.Value}");
r = new Regex(@"(\d+)-(\d+)-(\d+)");
Console.WriteLine(r.Replace(s, "$3-$1-$2"));
r = new Regex(@"\d+");
s = r.Replace(s, m2 => {
var arr = m2.Groups[0].Value.ToCharArray();
Array.Reverse(arr);
return new string(arr);
});
Console.WriteLine(s);
r = new Regex("%(?:begin|next|end)%");
s = "%begin%hello%next%world%end%";
Console.WriteLine(String.Join(",", r.Split(s)));
}
}
}
/*
Found matches:
group 0,0 : 123-4567-89
group 0,1 : 4567
group 1,0 : 987-6543-21
group 1,1 : 6543
89-123-4567,21-987-6543
321-7654-98,789-3456-12
,hello,world,
*/
C++
#include <iostream>
#include <string>
#include <vector>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
int main()
{
string s = "123-4567-89,987-6543-21";
boost::regex r(R"(\d{3}-(\d{4})-\d{2})");
boost::match_results<string::const_iterator> mr;
if (boost::regex_search(s, mr, r))
cout << "Found matches:" << endl;
int i = 0;
for (boost::sregex_iterator begin(s.begin(), s.end(), r), end, it = begin; it != end; ++it, ++i)
for (size_t j = 0; j < it->size(); ++j)
cout << "group " << i << "," << j << " : " << (*it)[j] << endl;
boost::regex r2(R"((\d+)-(\d+)-(\d+))");
cout << boost::regex_replace(s, r2, "$3-$1-$2") << endl;
boost::regex r3(R"(\d+)");
s = boost::regex_replace(s, r3, [](auto& match) {
string s = match.str();
reverse(s.begin(), s.end());
return s;
});
cout << s << endl;
boost::regex r4("%(?:begin|next|end)%");
s = "%begin%hello%next%world%end%";
vector<string> v;
for (boost::sregex_token_iterator begin(s.begin(), s.end(), r4, -1), end, i = begin; i != end; ++i)
v.push_back(i->str());
cout << boost::algorithm::join(v, ",") << endl;
}
/*
Found matches:
group 0,0 : 123-4567-89
group 0,1 : 4567
group 1,0 : 987-6543-21
group 1,1 : 6543
89-123-4567,21-987-6543
321-7654-98,789-3456-12
,hello,world
*/
|
请发表评论