Difference between revisions of "Introduction to Regular Expressions in Python"

From Sustainability Methods
Line 20: Line 20:
  
 
==Regular expressions` patterns==
 
==Regular expressions` patterns==
 +
 +
===Special characters:===
 +
 +
. - any charachter, except newline (\n)<br>
 +
^ - start of the string or line<br>
 +
$ - end of the string<br>
 +
\* - the previous charachter repeats 0 or more times =={0,}<br>
 +
\+ - the previous charachter repeats 1 or more times =={1,}<br>
 +
? - the presence of the previous charachter is not necessary =={0,1}<br> 
 +
{n, m} - the previous charachter repeats from n till m times<br> 
 +
{n} - the previous charachter repeats exactly n times<br>

Revision as of 12:49, 14 May 2024

THIS ARTICLE IS STILL IN EDITING MODE

Introduction

Regular expressions are sequences of characters that form a search pattern, mainly used for string searching and manipulation. In Python, they are implemented through the built-in 're' module.

Basic Concepts

  • Patterns: Specific sequences of characters that represent a search criteria.
  • Metacharacters: Special characters that signify broader types of patterns (like '*', '+', '?').
  • Character Classes: Represent a set of characters that match any one character from the set (like '\d', '\w', '\s').

Use Cases

  • String Parsing: Extracting specific information from text.
  • Data Validation: Ensuring formats of data are correct (like emails, phone numbers, passwords).
  • Text Preprocessing: Used in natural language processing for tasks like tokenization, cleaning data.

Regular expressions` patterns

Special characters:

. - any charachter, except newline (\n)
^ - start of the string or line
$ - end of the string
\* - the previous charachter repeats 0 or more times =={0,}
\+ - the previous charachter repeats 1 or more times =={1,}
? - the presence of the previous charachter is not necessary =={0,1}
{n, m} - the previous charachter repeats from n till m times
{n} - the previous charachter repeats exactly n times