regex

regex

s—
id: “regex”
aliases:
- “Usage”
- “Syntax”
tags:
- “regex”
- “python”

Syntax

regex101
regexr

Usage

Use re

1
2
3
re.sub(pattern,repl,str)
re.search(pattern, str)
re.findall(pattern,str)

Use Pandas

1
2
3
df['column_name'].str.contains('pattern')
df['column_name'].str.findall('pattern')
df['column_name'].str.replace('pattern', 'replacement')

Example

Example 1

Extracting room numbers from a ‘Description’ column in a DataFrame using regular expressions:

  1. Import the re module:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import re
    ````

    2. Define a function to extract the room number from a description:

    ```python
    def extract_room_number(description):
    match = re.search(r'(\d+\.\d+|\d+)(?=\s+of which are bedrooms)', description)
    if match:
    return float(match.group(1))
    else:
    return None
  2. Use the apply() method to apply the function to the ‘Description’ column and create a new ‘RoomNumber’ column:

1
df['RoomNumber'] = df['Description'].apply(extract_room_number)

This will extract the room number from the ‘Description’ column and store it in the new ‘RoomNumber’ column.

Author

Chen Yulin

Posted on

2023-05-10

Updated on

2024-05-15

Licensed under

Comments