![]() ![]() Result = re.findall(r"\Bthon", target_str)Īnd indeed, we have a match of "thon" inside the word “Python” not being at the beginning of the word. Target_str = "Jessa salary is 8000$ She is Python developer" So ‘ thon‘ should be part of a larger word in our string, but not at the beginning of the word. So this sequence can be useful for matching and locating some strings in a specific word.įor example, let’s use \B to check whether the string ‘ thon‘ is inside the target string but not at the beginning of a word. On the other hand, the special sequence \B matches the empty string or the border between two alphanumeric characters or two non-alphanumeric characters only when it is not at the beginning or at the end of a word. This sequence is the exact opposite of \b. Therefore keep in mind that the word you’re trying to match with the help of the \b special sequence should be separate, not part of a word. Moreover, the \b sequence always matches the empty string or boundary between an alphanumeric character and a non-alphanumeric character. But we will not get a match because non-alphanumeric characters do not border it on both sides. No match will be returned if the word is contained inside another word.įor instance, considering the same target string, we can search for the word “ ssa” using a \b special sequence like this "\bssa\b". One essential thing to keep in mind here is that the match will be made only for the complete and separate word itself. Result = re.findall(r"\bthon\b", target_str) # \b need separate word not part of a word # \w to match all alphanumeric characters Use a special sequence \W inside a regex pattern to find all the non-alphanumeric characters.Use a special sequence \w inside a regex pattern to find all alphanumeric character in the string.You can write \W using character class.Any character in the target string that is not alphanumeric would be the equivalent of the \W.This sequence is the exact opposite of \w, i.e., It matches any NON-alphanumeric character.This includes lowercase and uppercase letters, the digits 0 to 9, and the underscore character.The \w matches any alphanumeric character, also called a word character. ![]() Use a special sequence \D inside a regex pattern to find all the non-digit characters.Use a special sequence \d inside a regex pattern to find a 4-digit number in our target string.Also, you can write \D using character class (caret ^ at the beginning of the character class denotes negation).Any character in the target string that is not a digit would be the equivalent of the \D.This sequence is the exact opposite of \d, and it matches any non-digit character.This special sequence is equivalent to character class.The \d matches any digits from 0 to 9 inside the target string.Result = re.findall(r"\d.*?\Z", target_str) Target_str = "Jessa is a Python developer, and her salary is 8000" It works the same as the dollar ($) metacharacter. On the other hand, if we do have a multi-line string, then \A will still match only at the beginning of the string, while the caret will match at the beginning of each new line of the string.īackslash Z ( \Z ) sequences only match the end of the string. It works the same as the caret ( ^) metacharacter. The \A sequences only match the beginning of the string. Now Let’s see how to use each special sequence and character classes in Python regular expression. Ranges: matches a letter between m and p and digits from 2 to 8, but not p2 Match any letter except a, b, or c (negation) Match letter a or b or c followed by either p or q. Let’s see some of the most common character classes used inside regular expression patterns. įor example, it means match any lowercase letter from a to z. In Python, regex character classes are sets of characters or ranges of characters enclosed by square brackets. Python regex Special Sequences Character classes Matches the empty string, but only when it is not at the beginning or end of a word įor example, ‘ \bJessa\b' matches ‘Jessa’, ‘Jessa.’, ‘(Jessa)’, ‘Jessa Emma Kelly’ but not ‘JessaKelly’ or ‘Jessa5’. Matches a word boundary where a word character is. Matches the empty string, but only at the beginning or end of a word. Matches pattern only at the end of the string Matches pattern only at the start of the string The special sequences consist of '\' (backlash) and a character from the table below. Let’s see the list of regex special sequences and their meaning. Each special sequence makes specific common patterns more comfortable to use.įor example, you can use \d sequence as a simplified definition for character class, which means match any digit from 0 to 9. The special sequence represents the basic predefined character classes, which have a unique meaning. Use ranges to construct character classes.Use negation to construct character classes.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |