Home > Blockchain >  How to find substring in std::string using regular expression
How to find substring in std::string using regular expression

Time:06-08

I need to find in source string where substring must be template:
<any_symbols><any_number>_<any_number><any_symbols>

std::string source_sring = "SomeSymbolsN_NSomeSymbols" where N_N can be any variants. For example: 7_6, 4_2, 11_3, 1_2, ...

CodePudding user response:

For a simple regex like this you could construct your own state machine. You iterate over your symbols and update the state accordingly.
In this case you would want 3 states:

  • 0: Nothing found
  • 1: Last Symbol was a number
  • 2: Last Symbol was a lowdash preceded by a number

The logic behind switching states is:

  • If the current symbol is not a number or a dash: Go to state 0
  • If the current symbol is a number:
    • If the current state is 2: Accept number
    • Else: Go to state 1
  • If the current symbol is a lowdash:
    • If the current state is 1: Go to state 2
    • Else: Go to state 0

In code, this would look something like this (although the state variable should be documented or an enum):

bool matching = false;
int state = 0;
for (auto& c : s) {
    if (isdigit(c)) {
        if (state == 2) {
            matching = true;
            break;
        }
        else
            state = 1;
    }
    else if (c == '_') {
        if (state == 1)
            state = 2;
        else
            state = 0;
    }
    else {
        state = 0;
    }
}

For a more complex regex, I would suggest using a regex library, in this case the regex would be ".*\d_\d.*"

  • Related