IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 6: Python › CS105: Introduction to Python by Saylor Academy › Unit 8: Regular Expressions › Matching each regular expression predefined character class with its description
- This topic is empty.
-
AuthorPosts
-
August 30, 2024 at 2:16 pm #3322
Source: Created with help of AI tool
- Any decimal digit 0…9
The regular expression predefined character class for matching any decimal digit (0-9) is
\d
.\d
: Matches any single digit from 0 to 9. It is equivalent to the character set[0-9]
.
For example:
– The pattern\d
will match the digits in the string “abc123”, resulting in the matches “1”, “2”, and “3”.2. Any character that is not a digit
The regular expression predefined character class for matching any character that is not a digit is
\D
.\D
: Matches any character that is not a decimal digit (i.e., anything other than 0-9). It is the negation of\d
.
For example:
– The pattern\D
will match non-digit characters in the string “abc123”, resulting in the matches “a”, “b”, and “c”.3. Any whitespace character such as space, tab, or the newline character
The regular expression predefined character class for matching any whitespace character such as space, tab, or newline is
\s
.\s
: Matches any whitespace character, including spaces, tabs (\t
), newlines (\n
), and other Unicode whitespace characters.
For example:
– The pattern\s
will match spaces in the string “hello world”, resulting in the match of the single space character between “hello” and “world”.4. Any non-whitespace character
The regular expression predefined character class for matching any non-whitespace character is
\S
.\S
: Matches any character that is not a whitespace character. This includes letters, digits, punctuation, and symbols. It is the negation of\s
.
For example:
– The pattern\S
will match all non-whitespace characters in the string “hello world”, resulting in matches for “h”, “e”, “l”, “l”, “o”, “w”, “o”, “r”, “l”, “d”. It will skip the space between “hello” and “world”.5. Any alphanumeric character
The regular expression predefined character class for matching any alphanumeric character (letters and digits) is
\w
.\w
: Matches any alphanumeric character, including letters (uppercase and lowercase), digits (0-9), and the underscore (_
). It is equivalent to the character set[A-Za-z0-9_]
.
For example:
– The pattern\w
will match alphanumeric characters in the string “hello_world123”, resulting in matches for “h”, “e”, “l”, “l”, “o”, “_”, “w”, “o”, “r”, “l”, “d”, “1”, “2”, “3”.6. Any non-alphanumeric character
The regular expression predefined character class for matching any non-alphanumeric character is
\W
.\W
: Matches any character that is not an alphanumeric character (not a letter, digit, or underscore). It is the negation of\w
.
For example:
– The pattern\W
will match non-alphanumeric characters in the string “[email protected]!”, resulting in matches for “@”, “.”, and “!”. -
AuthorPosts
- You must be logged in to reply to this topic.