简体   繁体   English

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

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

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

My desired output are: 我想要的输出是:

12
13
14

How to write a pattern (in C++ or Java) to find all numbers between string " World{ " and " : "? 如何编写模式(使用C ++或Java)以查找字符串“ World { ”和“ ”之间的所有数字?

Use lookarounds or capturing group. 使用环视或捕获组。

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

or 要么

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

You can try this: 您可以尝试以下方法:

#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