snippetcppMinor
Parse regular expression
Viewed 0 times
expressionregularparse
Problem
Task:
This function searches given null terminated string
regular expression
Return value is matched string appeared in
If nothing has matched, return value is empty string.
The subset of regular expression grammar is defined as follows:
appeared next used as is.
of literal characters or metacharacters.
Immediately after
be followed. The num is a sequence of digits which specifies the number of
times the previous literal character or metacharacter sequence in squared
brackets must be repeated.
e.g. "
e.g. "
"
function returns a string "
My Solution:
Define a state machine and put regular expression strings into stacks. When used then pop up, multiple by
Code:
```
string PatternSearch(const unsigned char pStr, const unsigned char pMatch)
{
enum status {
normal,
escape,
repeatStrStart,
repeatStrEnd,
repeatNumStart
};
status current = normal;
status restore = normal;
const static string strErr = "ERROR";
stringstream bufNormal;
stringstream bufRepeat;
stringstream bufTm
This function searches given null terminated string
pStr by given subset ofregular expression
pMatch.Return value is matched string appeared in
pStr.If nothing has matched, return value is empty string.
The subset of regular expression grammar is defined as follows:
- A string is set of 8-bit characters. Beware that it is not 7-bit.
- The metacharacter
\is an escape character, that means any single letter
appeared next used as is.
- The characters in squared brackets metacharacters
[and]is a sequence
of literal characters or metacharacters.
Immediately after
], another metacharacters in curly braces { num } mustbe followed. The num is a sequence of digits which specifies the number of
times the previous literal character or metacharacter sequence in squared
brackets must be repeated.
e.g. "
abc[def]{2}ghi" matches "abcdefdefghi".- The squared brackets metacharacters can be nested.
e.g. "
abc[def[ghi]{3}jkl]{2}mno" matches"
abcdefghighighijkldefghighighijklmno".- In case of matching pattern given by
pMatchis a grammatical error, this
function returns a string "
ERROR".My Solution:
Define a state machine and put regular expression strings into stacks. When used then pop up, multiple by
N times and push to stack again.escape repeatStrStart --> repeatStrEnd --> repeatNumStart --
^ ^ | |
| | | |
| ------------ |
----------------------------------------------------Code:
```
string PatternSearch(const unsigned char pStr, const unsigned char pMatch)
{
enum status {
normal,
escape,
repeatStrStart,
repeatStrEnd,
repeatNumStart
};
status current = normal;
status restore = normal;
const static string strErr = "ERROR";
stringstream bufNormal;
stringstream bufRepeat;
stringstream bufTm
Solution
Speaking from a purely style point of view, nine levels of indentation is a substantial red flag, when you should be able to keep it at three.
So much indentation makes this code hard to read in the first place, which is not the sort of practice you want to get into, especially if your code will be read and evaluated by others.
So much indentation makes this code hard to read in the first place, which is not the sort of practice you want to get into, especially if your code will be read and evaluated by others.
Context
StackExchange Code Review Q#134699, answer score: 2
Revisions (0)
No revisions yet.