Python Regex - RegEx Functions | Metacharacters | Special SequencesA regular expression is a set of characters with highly specialized syntax that we can use to find or match other characters or groups of characters. In short, regular expressions, or Regex, are widely used in the UNIX world. Import the re ModuleThe re-module in Python gives full support for regular expressions of Pearl style. The re module raises the re.error exception whenever an error occurs while implementing or using a regular expression. We'll go over crucial functions utilized to deal with regular expressions. But first, a minor point: many letters have a particular meaning when utilized in a regular expression called metacharacters. The majority of symbols and characters will easily match. (A case-insensitive feature can be enabled, allowing this RE to match Python or PYTHON.) For example, the regular expression 'check' will match exactly the string 'check'. There are some exceptions to this general rule; certain symbols are special metacharacters that don't match. Rather, they indicate that they must compare something unusual or have an effect on other parts of the RE by recurring or modifying their meaning. Metacharacters or Special CharactersAs the name suggests, there are some characters with special meanings:
Special Sequences:The ability to match different sets of symbols will be the first feature regular expressions can achieve that's not previously achievable with string techniques. On the other hand, Regexes isn't much of an improvement if that had been their only extra capacity. We can also define that some sections of the RE must be reiterated a specified number of times. The first metacharacter we'll examine for recurring occurrences is *. Instead of matching the actual character '*,' * signals that the preceding letter can be matched 0 or even more times rather than exactly once. Ba*t, for example, matches 'bt' (zero 'a' characters), 'bat' (one 'a' character), 'baaat' (three 'a' characters), etc. Greedy repetitions, such as *, cause the matching algorithm to attempt to replicate the RE as many times as feasible. If later elements of the sequence fail to match, the matching algorithm will retry with lesser repetitions. Special Sequences consist of '\' followed by a character listed below. Each character has a different meaning.
RegEx Functions:
1. re.compile(pattern, flags=0)It is used to create a regular expression object that can be used to match patterns in a string. Example: Output: Match Object: This is equivalent to:
Note - When it comes to using regular expression objects several times, the re.complie() version of the program is much more efficient.2. re.match(pattern, string, flags=0)
Parameters
Example: Output:
Another example of the implementation of the re.match() method in Python.
CODE: Output: There isn't any match!! 3. re.search(pattern, string, flags=0)The re.search() function will look for the first occurrence of a regular expression sequence and deliver it. It will verify all rows of the supplied string, unlike Python's re.match(). If the pattern is matched, the re.search() function produces a match object; otherwise, it returns "null." To execute the search() function, we must first import the Python re-module and afterward run the program. The "sequence" and "content" to check from our primary string are passed to the Python re.search() call. Here is the description of the parameters - pattern:- this is the expression that is to be matched. It must be a regular expression string:- The string provided is the one that will be searched for the pattern wherever within it. flags:- Bitwise OR (|) can be used to express multiple flags. These are modifications, and the table below lists them. Code Output: search object group : Python through tutorials on javatpoint search object group 1 : on search object group 2 : javatpoint 4. re.sub(pattern, repl, string, count=0, flags=0)
Example 1: Output: Original text: I like Javatpoint! Substituted text: I love Javatpoint! In the above example, the sub-function replaces the 'like' with 'love'. Example 2 - Substituting 3 occurrences of a pattern. Output: Original text: I like Javatpoint! I also like tutorials! Substituted text: I Like Javatpoint! I aLso Like tutorials! Here, first three occurrences of 'l' is substituted with the "L". 5. re.subn(pattern, repl, string, count=0, flags=0)
Example: Output: Original text: I like Javatpoint! I also like tutorials! Substituted text: ('I Like Javatpoint! I aLso Like tutorials!', 3) In the above program, the subn function replaces the first three occurrences of 'l' with 'L' in the string. 6. re.fullmatch(pattern, string, flags=0)
Example: Output: None In the above program, only the 'Hello world" has completely matched the pattern, not 'Hello'. Q. When to use re.findall()? Ans. Suppose we have a line of text and want to get all of the occurrences from the content, so we use Python's re.findall() function. It will search the entire content provided to it. 7. re.finditer(pattern, string, flags=0)
Output: 8. re.split(pattern, string, maxsplit=0, flags=0)
Example: Output: When maxsplit = 0, result: ['Learn', 'Python', 'through', 'tutorials', 'on', 'javatpoint'] When maxsplit = 1, result = ['Learn', 'Python through tutorials on javatpoint'] 9. re.escape(pattern)
Example: Output: Result: https://www\.javatpoint\.com/ The escape function escapes the metacharacter '.' from the pattern. This is useful when want to treat metacharacters as regular characters to match the actual characters themselves. 10. re.purge()
Example: Output:
Matching Versus Searching - re.match() vs. re.search()Python has two primary regular expression functions: match and search. The match function looks for a match only where the string starts, whereas the search function looks for a match everywhere in the string. CODE: Output: There isn't any match!! Search object group : The match function checks whether the string is starting with 'through' or not, and the search function checks whether there is 'through' in the string or not. CONCLUSIONThe re-module in Python supports regular expression. Regular expressions are an advanced tool for text processing and pattern matching. We can find patterns in text strings using the re-module and split and replace text depending on patterns, among other things. Also, using the re-package isn't always a good idea. If we're only searching a fixed string or a specific character class and not leveraging any re-features like the IGNORECASE flag, regular expressions' full capability would not be needed. Strings offer various ways of doing tasks with fixed strings, and they're generally considerably faster than the larger, more generalized regular expression solver because the execution is a simple short C loop that has been optimized for the job. Next TopicPython OOPs Concepts |