


# Try substitute: re.sub( regexStr, replacementStr, inStr) -> outStr # r'.' denotes raw strings which ignore escape code, i.e., r'\n' is '\'+'n' > import re # Need module 're' for regular expression # Try find: re.findall( regexStr, inStr) -> matchedSubstringsList # Test under the Python Command-Line Interpreter Python also uses backslash ( \) for escape sequences (i.e., you need to write \\ for \, \\d for \d), but it supports raw string in the form of r'.', which ignore the interpretation of escape sequences - great for writing regex.

REGEX FOR NUMBER OF LINES FULL
See " Python's re module for Regular Expression" for full coverage.
REGEX FOR NUMBER OF LINES CODE
There are more than one ways to write a regex! Take note that many programming languages (C, Java, JavaScript, Python) use backslash \ as the prefix for escape sequences (e.g., \n for newline ), and you need to write "\\d+" instead.Ĭode Examples (Python, Java, JavaScript, Perl, PHP) Code Example in Python

In this example, matches any SINGLE character between 0 and 9 (i.e., a digit), where dash ( -) denotes the range. It matches any SINGLE character in the list. The, known as character class (or bracket list), encloses a list of characters.A regex ( regular expression) consists of a sequence of sub-expressions.E.g., the regex x matches substring "x" regex 9 matches "9" regex = matches "=" and regex matches Special Regex Characters: These characters have special meaning in regex (to be discussed below). Character: All characters, except those having special meaning in regex, matches themselves.For novices, go to the next section to learn the syntax, before looking at these examples. This section is meant for those who need to refresh their memory. Getting started with regex may not be easy due to its geeky syntax, but it is certainly worth the investment of your time. Regex is supported in all the scripting languages (such as Perl, Python, PHP, and JavaScript) as well as general purpose programming languages such as Java and even word processors such as Word for searching texts. One line of regex can easily replace several dozen lines of programming codes. Regular Expression, or regex or regexp in short, is extremely and amazingly powerful in searching and manipulating text strings, particularly in processing text files.
