如何使用正则表达式查找特殊字符串之间的所有数字

How to use regular expression to find all numbers between special strings?

本文关键字:之间 数字 字符串 何使用 正则表达式 查找      更新时间:2023-10-16

给定字符串:HelloWorld{12:777}ByeWorld{13:888}OkayWorld{14:999}

我希望的输出是:

12
13
14

如何写一个模式(在c++或Java中)找到字符串"World{"answers":"之间的所有数字?

使用查找或捕获组

Pattern.compile("(?<=World\{)[^:]*(?=:)");

Pattern.compile("\bWorld\{([^:]*):");

你可以试试:

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main(void)
{
    char ch[100];
    scanf("%s", ch);
    char *token;
    token = strtok(ch, "{");
    token = strtok(NULL, ":");
    cout<< token << endl;
    return 0;
}