fnmatch实例详解(与readdir、opendir实现模糊查询)

fnmatch:int fnmatch(const char *pattern, const char *string, int flags);
man中是这么写道:The  fnmatch()  function checks whether the string argument matches the pattern argument, which is a shell wildcard pattern. 就是判断字符串是不是符合pattern所指的结构。
  

FNM_NOESCAPE
如果这个标志设置了,处理反斜杠为普通字符,而不是转义字符。
FNM_PATHNAME
如果这个标志设置了,string 里的斜杠只匹配 pattern 里的斜杠,它不能匹配星号(*)或问号(?)元字符,也不能匹配包含斜杠的中括号表达式([])。
FNM_PERIOD
如果这个标志设置了,string 里的起始点号必须匹配 pattern 里的点号。一个点号被认为是起始点号,如果它是string 第一个字符,或者如果同时设置了 FNM_PATHNAME,紧跟在斜杠后面的点号。
FNM_FILE_NAME
这是 FNM_PATHNAME 的 GNU 同义语。
FNM_LEADING_DIR
如果这个标志(GNU 扩展)设置了,模式必须匹配跟随在斜杠之后的 string 的初始片断。这个标志主要是给 glibc 内部使用并且只在一定条件下实现。
FNM_CASEFOLD
如果这个标志(GNU 扩展)设置了,模式匹配忽略大小写。

返回值:0,string 匹配 pattern;FNM_NOMATCH,没有匹配;或者其它非零值,如果出错。

(程序来自网络)

#include <fnmatch.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
	char *pattern;
	DIR *dir;
 	struct dirent *entry;
	int ret;

	dir = opendir(argv[2]);	//打开指定路径

	pattern = argv[1];	
	
	//路径存在
	if(dir != NULL)
	{
		//逐个获取文件夹中文件
	    while( (entry = readdir(dir)) != NULL)
		{
	      ret = fnmatch(pattern, entry->d_name, FNM_PATHNAME|FNM_PERIOD);
	      if(ret == 0)			//符合pattern的结构
			{
	        printf("%s\n", entry->d_name);
	      }else if(ret == FNM_NOMATCH){
	        continue ;
	      }else
			{
	        printf("error file=%s\n", entry->d_name);
	    		}
	    	}

    	closedir(dir);
  	}
}


 

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python模块详解 各个模块的详解 核心模块 1.1. 介绍 1.2. _ _builtin_ _ 模块 1.3. exceptions 模块 1.4. os 模块 1.5. os.path 模块 1.6. stat 模块 1.7. string 模块 1.8. re 模块 1.9. math 模块 1.10. cmath 模块 1.11. operator 模块 1.12. copy 模块 1.13. sys 模块 1.14. atexit 模块 1.15. time 模块 1.16. types 模块 1.17. gc 模块 更多标准模块 2.1. 概览 2.2. fileinput 模块 2.3. shutil 模块 2.4. tempfile 模块 2.5. StringIO 模块 2.6. cStringIO 模块 2.7. mmap 模块 2.8. UserDict 模块 2.9. UserList 模块 2.10. UserString 模块 2.11. traceback 模块 2.12. errno 模块 2.13. getopt 模块 2.14. getpass 模块 2.15. glob 模块 2.16. fnmatch 模块 2.17. random 模块 2.18. whrandom 模块 2.19. md5 模块 2.20. sha 模块 2.21. crypt 模块 2.22. rotor 模块 2.23. zlib 模块 2.24. code 模块 线程和进程 3.1. 概览 3.2. threading 模块 3.3. Queue 模块 3.4. thread 模块 3.5. commands 模块 3.6. pipes 模块 3.7. popen2 模块 3.8. signal 模块 数据表示 4.1. 概览 4.2. array 模块 4.3. struct 模块 4.4. xdrlib 模块 4.5. marshal 模块 4.6. pickle 模块 4.7. cPickle 模块 4.8. copy_reg 模块 4.9. pprint 模块 4.10. repr 模块 4.11. base64 模块 4.12. binhex 模块 4.13. quopri 模块 4.14. uu 模块 4.15. binascii 模块 文件格式 5.1. 概览 5.2. xmllib 模块 5.3. xml.parsers.expat 模块 5.4. sgmllib 模块 5.5. htmllib 模块 5.6. htmlentitydefs 模块 5.7. formatter 模块 5.8. ConfigParser 模块 5.9. netrc 模块 5.10. shlex 模块 5.11. zipfile 模块 5.12. gzip 模块 邮件和新闻消息处理 6.1. 概览 6.2. rfc822 模块 6.3. mimetools 模块 6.4. MimeWriter 模块 6.5. mailbox 模块 6.6. mailcap 模块 6.7. mimetypes 模块 6.8. packmail 模块 6.9. mimify 模块 6.10. multifile 模块 网络协议 7.1. 概览 7.2. socket 模块 7.3. select 模块 7.4. asyncore 模块 7.5. asynchat 模块 7.6. urllib 模块 7.7. urlparse 模块 7.8. cookie 模块 7.9. robotparser 模块 7.10. ftplib 模块 7.11. gopherlib 模块 7.12. httplib 模块 7.13. poplib 模块 7.14. imaplib 模块 7.15. smtplib 模块 7.16. telnetlib 模块 7.17. nntplib 模块 7.18. SocketServer 模块 7.19. BaseHTTPServer 模块 7.20. SimpleHTTPServer 模块 7.21. CGIHTTPServer 模块 7.22. cgi 模块 7.23. webbrowser 模块 国际化 8.1. locale 模块 8.2. unicodedata 模块 8.3. ucnhash 模块 多媒体相关模块 9.1. 概览 9.2. imghdr 模块 9.3. sndhdr 模块 9.4. whatsound 模块 9.5. aifc 模块 9.6. sunau 模块 9.7. sunaudio 模块 9.8. wave 模块 9.9. audiodev 模块 9.10. winsound 模块 数据储存 10.1. 概览 10.2. anydbm 模块 10.3. whichdb 模块 10.4. shelve 模块 10.5. dbhash 模块 10.6. dbm 模块 10.7. dumbdbm 模块 10.8. gdbm 模块 工具和实用程序 11.1. dis 模块 11.2. pdb 模块 11.3. bdb 模块 11.4. profile 模块 11.5. pstats 模块 11.6. tabnanny 模块 其他模块 12.1. 概览 12.2. fcntl 模块 12.3. pwd 模块 12.4. grp 模块 12.5. nis 模块 12.6. curses 模块 12.7. termios 模块 12.8. tty 模块 12.9. resource 模块 12.10. syslog 模块 12.11. msvcrt 模块 12.12. nt 模块 12.13. _winreg 模块 12.14. posix 模块 执行支持模块 13.1. dospath 模块 13.2. macpath 模块 13.3. ntpath 模块 13.4. posixpath 模块 13.5. strop 模块 13.6. imp 模块 13.7. new 模块 13.8. pre 模块 13.9. sre 模块 13.10. py_compile 模块 13.11. compileall 模块 13.12. ihooks 模块 13.13. linecache 模块 13.14. macurl2path 模块 13.15. nturl2path 模块 13.16. tokenize 模块 13.17. keyword 模块 13.18. parser 模块 13.19. symbol 模块 13.20. token 模块 其他模块 14.1. 概览 14.2. pyclbr 模块 14.3. filecmp 模块 14.4. cmd 模块 14.5. rexec 模块 14.6. Bastion 模块 14.7. readline 模块 14.8. rlcompleter 模块 14.9. statvfs 模块 14.10. calendar 模块 14.11. sched 模块 14.12. statcache 模块 14.13. grep 模块 14.14. dircache 模块 14.15. dircmp 模块 14.16. cmp 模块 14.17. cmpcache 模块 14.18. util 模块 14.19. soundex 模块 14.20. timing 模块 14.21. posixfile 模块 14.22. bisect 模块 14.23. knee 模块 14.24. tzparse 模块 14.25. regex 模块 14.26. regsub 模块 14.27. reconvert 模块 14.28. regex_syntax 模块 14.29. find 模块
### 回答1: 在 Python 中,可以使用模糊查询来模糊匹配字符串。常用的模糊匹配方法有如下几种: 1. 使用 re 模块中的 re.search() 方法进行正则表达式匹配: ```python import re str1 = "hello world" match = re.search("he.*ld", str1) if match: print(match.group()) # 输出 "hello world" ``` 在上面的例子中,使用 re.search() 方法进行正则表达式匹配,匹配成功后返回一个 Match 对象,通过 group() 方法可以获取匹配到的字符串。 2. 使用 fnmatch 模块中的 fnmatch() 方法进行通配符匹配: ```python import fnmatch str1 = "hello world" if fnmatch.fnmatch(str1, "he*ld"): print(str1) # 输出 "hello world" ``` 在上面的例子中,使用 fnmatch() 方法进行通配符匹配,匹配成功后返回 True,表示匹配成功。 3. 使用 difflib 模块中的 get_close_matches() 方法进行模糊匹配: ```python import difflib str1 = "hello world" matches = difflib.get_close_matches("held", [str1]) if matches: print(matches[0]) # 输出 "hello world" ``` 在上面的例子中,使用 get_close_matches() 方法进行模糊匹配,返回与目标字符串最接近的匹配项,如果没有匹配项,则返回空列表。 需要注意的是,以上三种方法都有各自的适用范围和限制,需要根据具体的需求选择合适的方法。 ### 回答2: Python的模糊查询是指在搜索或匹配字符串时,可以忽略一些特定的字符或模式,从而找到相似或相关的结果。可以利用Python中的各种方法和库来实现模糊查询。 一种常用的模糊查询方法是使用正则表达式。Python的re模块提供了用于处理正则表达式的函数和方法。通过使用正则表达式的特定模式,可以在字符串中进行模糊匹配。例如,可以使用通配符符号(如*)代替特定字符或模式,从而匹配多种可能的结果。 另一种常用的模糊查询方法是使用模糊匹配库,如fuzzywuzzy。该库可以通过计算字符串之间的相似度来进行模糊匹配。它提供了各种函数和方法,可以根据不同的需求来进行模糊查询。例如,可以使用fuzzywuzzy库中的process.extract函数来找到与目标字符串最相似的结果。 除了以上两种方法,还可以使用Python中其他的字符串处理函数和方法,如字符串比较函数、模式匹配函数等来实现模糊查询。具体的方法选择取决于具体的需求和匹配规则。 在使用Python进行模糊查询时,需要注意性能和效率问题。对于大型数据集或复杂的匹配模式,可能需要使用更高级的算法或进行优化,以提高查询速度和准确性。此外,还需要注意保护隐私数据和处理异常情况,确保查询结果的正确性和安全性。 ### 回答3: Python中的模糊查询通常是指根据模糊条件来搜索和匹配数据。在Python中,我们可以使用不同的方法来实现模糊查询。 一种常用的方法是使用正则表达式。Python的re模块提供了用于处理正则表达式的函数和方法。我们可以使用re模块提供的模糊匹配函数,例如re.match()、re.search()和re.findall(),来为要查询的模式创建正则表达式,并对数据进行搜索和匹配。 另一种常用的方法是使用字符串方法。Python的字符串提供了一些用于模糊查询的方法,例如find()、startwith()和endswith()。这些方法允许我们查找包含特定字符或字符串的子字符串,并返回其位置或匹配结果。 此外,还有一些基于模糊查询的库和工具可以在Python中使用。例如,FuzzyWuzzy库提供了模糊匹配字符串的功能,它可以计算字符串之间的相似度并返回最佳匹配结果。 总之,在Python中实现模糊查询可以通过正则表达式、字符串方法或使用相应的库和工具来完成。我们可以根据实际需求选择合适的方法,并结合适当的算法和逻辑来达到我们想要的查询结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值