繁体   English   中英

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

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

输入一个字符串: HelloWorld {12:777} ByeWorld {13:888} OkayWorld {14:999}

我想要的输出是:

12
13
14

如何编写模式(使用C ++或Java)以查找字符串“ World { ”和“ ”之间的所有数字?

使用环视或捕获组。

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM