In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "python regular expression parameter replacement case analysis", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "python regular expression parameter replacement case analysis"!
A regular expression is a special sequence of characters that helps you easily check whether a string matches a pattern.
Python has added the re module since version 1.5, which provides Perl-style regular expression patterns.
The re module enables the Python language to have all the regular expression functions.
The compile function generates a regular expression object based on a pattern string and optional flag parameters. The object has a series of methods for regular expression matching and replacement.
The re module also provides functions that are exactly the same as those of these methods, which take a pattern string as their first argument.
This section focuses on python regular expression parameter substitution.
Replace dictionary data with regular expressions
Regular expression'\ $\ {. *\} 'corresponding to ${memberID}
Explanation:\ $escape replacement string $\ {escape replacement string {. Except for any single self-character * match * in\ n, the characters preceding the character zero or more times\} escape in the replacement string}
Dict = {"memberId": "${memberID}", "password": "123456", "loanId": "${loanId}", "amount": "- 100"} data = {"memberId": 10001, "loanId": 1} for param in dict: value = dict [param] if param in data.keys (): s = str (data [param]) newValue = re.sub ('\ $\ {. *\}', s, value) dict [param] = newValueprint (dict)
Extend:
Common rules for regular expressions
^ matches the beginning of a string, specifies what the string must begin and ends with a matching string, and specifies that the string ends with a character before $+ matches the previous character once or more times > = 1 {m} specifies the number of matching characters? Match the previous characters 0 or 1 times
Regular expressions are handled by the re module in python. The common methods of the re module are as follows:
Re.match (re rules, strings, modifiers): match from scratch. The match starts with the first character of the string, and if the first character does not match the rule, the match fails.
Re.search (re rules, strings, modifiers): match contains. Matching from the first character of the string is not required. As long as there is a match in the string that matches the rule, the match is successful.
Re.findall (re rules, strings, modifiers): put all matching characters in the list and return.
Re.sub (re rule, replacement string, replaced string, number of substitutions, modifier): match characters and replace.
Thank you for your reading, the above is the content of "python regular expression parameter replacement example analysis". After the study of this article, I believe you have a deeper understanding of the problem of python regular expression parameter replacement example analysis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.