![]() ![]() Although the textual representation of the first matching object does not look pretty, it gives a clear hint that the given string 'crypto-bot that is trading Bitcoin' matches the regular expression. String text_1 matches the pattern (indicated by the resulting match object), string text_2 doesn’t (indicated by the result None). This leads to the following result: # Result Now, we call the function match() on our compiled pattern and the text to be searched. We say that the pattern is compiled because Python creates a pattern object that can be reused in multiple locations-much like a compiled program can be executed multiple times. Pattern = re.compile("crypto(.)coin matches the regex consisting of three parts: the word "crypto", an arbitrary sequence with 1 to 30 chars, followed by the word “coin”. Text_2 = "cryptographic encryption methods that can be cracked easily with quantum computers" Text_1 = "crypto-bot that is trading Bitcoin and other currencies" Let’s have a first look at the result before we discuss-in a step-by-step manner-how the code solves the problem. Our goal is to solve the following problem: Given a string, find occurrences where the string “crypto” is followed by up to 30 arbitrary characters, followed by the string "coin". For example, the set of query strings can even have an infinite size! Regular expressions are much more powerful than default textual search functionality as shown above.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |