DEV Community

Tejeswararao123
Tejeswararao123

Posted on • Edited on

Regular Expressions

Regular Expressions

  • Regular expression is a sequence of characters that defines a search pattern.
  • It is a impressive tool for searching a pattern.

Example

string_a = re.search(r'portal', 'portal for folks')
print(string_a)
print(string_a.group())
print('Start from the Index:', string_a.start())
print('Ends at the Index:', string_a.end())
Enter fullscreen mode Exit fullscreen mode

output:

<re.Match object; span=(0, 6), match='portal'>
portal
Start from the Index: 0
Ends at the Index: 6
Enter fullscreen mode Exit fullscreen mode

Meta Characters

The below table shows the list of MetaCharacters are used in regex
Image description

Applications

Data Mining:

  • Data mining is the process of sorting large amounts of data to identify patterns and relationships that can help solve business problems through data analysis.

Data Validation:

  • Data validation is the process of checking and validating data that is collected before it is used.

Basic regular expressions.

Character Classes

  • Character classes allow us to match a single character set against a possible character set. You can also place a character class inside the square brackets.

Ranges

  • The range provides the versatility to match a text in the range. The hyphen character within the character class represents a range.

Shortcuts in RegEx.

  • \w – matches a word character
  • \d – matches digit character
  • \s – matches white space character (space, tab, newline, etc.)
  • \b – matches a zero-length character

Negation

  • Negation inverts character classes. Finds a match excluding the reversed character or range of reversed characters specified in the character class.

Ends of String

  • The caret character matches the beginning of the string and the $ character matches at the end of the string.

Any Character

  • The period character represents any single character except the character classes enclosed in parentheses.

Top comments (0)