Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement glob style pattern with python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Python how to achieve glob style pattern, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

When it comes to wildcards, people will soon think of * and? Number, with wildcards, makes the ability of expression greatly enhanced, and many linux commands support this thing, which is actually glob style pattern.

Even redis's keys command supports glob.

The glob I'm going to implement supports the following features:

The asterisk * matches 0 or more arbitrary characters

? Matches an exact arbitrary character

[characters] matches any character in square brackets, such as [abc], either a, b, or c.

[! character] excludes characters in square brackets

[character-character], which means that all characters in the range of 2 characters can be matched, such as [a murz], [0-9]

To achieve this thing is actually quite simple, scan s and p strings from left to right, if they all come to the end, then it can be matched.

The main difficulty lies in the matching of the * sign. Because the * sign can match 0 or more, you need to try backtracking. Here, by saving the * position, if the later one can not get through, pull it back to the * position and match greedily.

As for the expansion of square brackets, it is clear to have include and exclude variables.

Code below.

# coding=utf-8def build_expand (p): # square brackets expand ptr2include = {} ptr2exclude = {} ptr2next = {} len_p = len (p) pPtr = 0 while pPtr

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report