简体   繁体   中英

Using RegEx to filter wrong Input?

Look at this example:

string str = "January 19934";

The Outcome should be

Jan 1993

I think I have created the right RegEx ([Az]{3}).*([\\d]{4}) to use in this case but I do not know what I should do now?

How can I extract what I am looking for, using RegEx? Is there a way like receiving 2 variables, the first one being the result of the first RegEx bracket: ([Az]{3}) and the second result being 2nd bracket: [[\\d]{4}] ?

Your regex contains a common typo: [Az] matches more than just ASCII letters . Also, the .* will grab all the string up to its end, and backtracking will force \\d{4} match the last 4 digits. You need to use lazy quantifier with the dot, *? .

Then, use regex_search and concat the 2 group values:

#include <regex>
#include <string>
#include <iostream>
using namespace std;

int main() {
    regex r("([A-Za-z]{3}).*?([0-9]{4})");
    string s("January 19934");
    smatch match;
    std::stringstream res("");
    if (regex_search(s, match, r)) {
        res << match.str(1) << " " << match.str(2);
    }
    cout << res.str();  // => Jan 1993
    return 0;
}

See the C++ demo

Pattern explanation :

  • ([A-Za-z]{3}) - Group 1: three ASCII letters
  • .*? - any 0+ chars other than line break symbols as few as possible
  • ([0-9]{4}) - Group 2: 4 digits

This could work.

([A-Za-z]{3})([a-z ])+([\d]{4})

Note the space after az is important to catch space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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