Sword refers to offer question 19-regular expression matching

Regular expression matching

Subject: Please implement a function to match regular expressions containing'.' and'*'. The character'.' in the pattern means any character, and'*' means that the character before it can appear any number of times (including 0 times). In this question, matching means that all characters in the string match the entire pattern. For example, the string "aaa" matches the patterns "aa" and "ab * ac * a" but matches "aa.a" and "ab * a" "None match

Each time a character is taken from the string to match the character in the pattern.

Suppose the string a is a matching string, and the string b is the pattern

How to match

1. If the character ch in the pattern b is'.' then it can match any character in the string
2. If the character ch in the pattern b is not'.', and the character in the string a is also ch, then they match each other, At this time, you need to match the following characters
3. When the following characters are not'*',
if the first character in the string a matches the first character in the pattern b, then both are moved backward, otherwise it returns false
4. When the following character is' *',
there may be multiple matching methods; the
first one, the pattern b moves two characters back and the string a does not move, which means that it matches 0 characters.
The second one is the string a moves back one character, mode b moves two characters backward or remains unchanged

Example-illustration

We can use the pattern ba*ab as an example to draw its non-deterministic priority state machine diagram, as follows:
Insert picture description here
When the match enters state 2 and the character of the string a is'a', there are two options:
1. Enter state 3. (Move back two characters on the mode)
2. Return to state 3 (the mode is kept unchanged)

Code

code show as below:

bool match(char* str,char* pattern)
{
    
    
	if(str==nullptr || pattern==nullptr)
		return false;
	return matchCore(str,pattern);
}

bool matchCore(char* str,char* pattern)
{
    
    
	if(*str=='\0' && *pattern=='\0')
		return true;
	
	if(*str!='\0' && *pattern=='\0')
		return false;
	
	if(*(pattern+1)=='*')
	{
    
    
		if(*pattern==*str || (*pattern=='.' && *str!='\0'))
			//移动到下一个状态
		return matchCore(str+1,pattern+2)
			//停留在当前状态
			|| matchCore(str+1,pattern)
			//忽略一个 '*'
			|| macthCore(str,pattern+2);
		else
			return matchCore(str,pattern+2);
	}
	if(*str==*patter || (*pattern=='.' && *str!='\0'))
		return matchCore(str+1,pattern+1);
	return false;
}

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104227664