比较通配符字符串 [英] Compare strings with wildcard

查看:145
本文介绍了比较通配符字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,我究竟可以比较包含字符串* (即可能是字符的任意组合)与字符串的二维矩阵?

例如,我有字去* S 。它可以生成的话华丽( * 是orgeou),善,山羊,云等我应该从读的话整个字典,并与包含一个或多个星号字加以比较( * )。可以从与星号的字中产生的每个字将不得不被打印。如果这两个词具有相同的长度那么很容易比较,因为 * 只能是一个字母。

  INT FQ(字符* S1,字符* S2){
INT I,A = 0,B = 0,S = 0;
而(1){
    如果(S1 [A] =='\\ 0')
        打破;
    一个++;
}
如果(strlen的(S1)== strlen的(S2)){
    对于(i = 0; I< A;我++){
        如果(S1 [Ⅰ] ==*){
            基础B ++;
            }
        如果(S1 [Ⅰ] == S2 [I]){
            基础B ++;
            }
        }
    }
如果(二==一)
    返回1;


解决方案

您可以很容易写一个递归函数通过检查字符模式字符串中的字符与通配符中有一个刺比较另一个字符串,并应用这些规则如下:


  • 如果模式[P] =='\\ 0':模式匹配,如果候选人[C] =='\\ 0'

  • 如果模式[P] =='*':尝试匹配的候选人[C] ...候选人[C + N]与图案[P + 1]

  • 如果模式[P]!='?'和模式[P] =候选[C]:暂无匹配

  • 否则,匹配模式[P + 1]的候选[C + 1]

这几个规则很容易被写成进行匹配的递归函数:

 的#include< stdbool.h>布尔匹配(为const char *模式,为const char *候选人,INT P,INT C){
  如果(模式[P] =='\\ 0'){
    返回候选[C] =='\\ 0';
  }否则如果(图案[P] ==*){
    对(候选[C] ='\\ 0';!C ++){
      如果(匹配(模式,候选人,P + 1,C))
        返回true;
    }
    返回匹配(模式,候选人,P + 1,C);
  }否则如果(图案[P] =&放大器!'?';&放大器;!图案[P] =候补并[c]){
    返回false;
  }其他{
    返回匹配(模式,候选人,P + 1,C + 1);
  }
}

那么,你可以这样做:

 匹配(F * O,富,0,0);

这是不是一种有效的方法,但我认为这是很容易理解和执行。 http://en.wikipedia.org/wiki/:如果你需要的东西更有效,你可以从这些开始String_searching_algorithm

In C, how exactly can I compare a string that contains * (that can be any combination of characters) with a two dimensional matrix of strings?

For example, I have the word go*s. It can generate the words "gorgeous" (* is "orgeou"), "goodness", "goats", "goes" etc. I am supposed to read the words from a whole dictionary and compare them with a word that contains one or more asterisks (*). Each word that can be generated from the word with the asterisk will have to be printed. If the two words have the same length then it is easy to compare because the * can only be one letter.

int fq(char *s1, char *s2){
int i, a=0, b=0, s=0;
while (1){
    if (s1[a]=='\0')
        break;
    a++;
}
if (strlen(s1)==strlen(s2)){
    for(i=0; i<a; i++){
        if (s1[i]=='*'){
            b++;
            }
        if (s1[i]==s2[i]){
            b++;
            }
        }
    }
if (b==a)
    return 1;

解决方案

You can fairly easily write a recursive function for comparing a sting to another string with a wildcard in it by examining the pattern string character by character and applying these rules as follows:

  • if pattern[p] == '\0': the patterns match if candidate[c] == '\0'
  • if pattern[p] == '*': try to match candidate[c]...candidate[c+n] with pattern[p+1]
  • if pattern[p] != '?' and pattern[p] != candidate[c]: No match
  • otherwise, match pattern[p+1] with candidate[c+1]

These few rules can easily be written as a recursive function for matching:

#include <stdbool.h>

bool match(const char *pattern, const char *candidate, int p, int c) {
  if (pattern[p] == '\0') {
    return candidate[c] == '\0';
  } else if (pattern[p] == '*') {
    for (; candidate[c] != '\0'; c++) {
      if (match(pattern, candidate, p+1, c))
        return true;
    }
    return match(pattern, candidate, p+1, c);
  } else if (pattern[p] != '?' && pattern[p] != candidate[c]) {
    return false;
  }  else {
    return match(pattern, candidate, p+1, c+1);
  }
}

then, you can do:

match("f*o", "foo", 0, 0);

This is not an effective method, but I think it is easy to understand and implement. If you need something more efficient, you can start from these: http://en.wikipedia.org/wiki/String_searching_algorithm

这篇关于比较通配符字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆