简体   繁体   中英

Python - Regular Expression pattern

I am trying to create function that will use the pattern like 12X15x2 and extract it from string like this: "STACKED STONE 52X36X72 AREAWELL BOMAN KEMP" So the pattern is dimensions winch are usually one or more digits that "X" than again one or more digits than "X" and finishing up with one or more digits.

Here is what i have tried:

# Importing dependencies
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import re

# Setting the test string
s = "STACKED STONE 52\"X36\"X72\" AREAWELL BOMAN KEMP"
# Test the pattern on a s string
result = re.sub(r"[a-z ]", "", s , flags=re.I) 
print(result)

As a results i get 52"36"72 which is just replacing letters in the string with nothing.

Based on my research I think i have to use compile function and define the pattern to read digits letter X digits letter X digits, so something like [1-9],"X",[1-9],"X",[1-9]

Any idea how to structure this and which function of re to use?

I have tried few solutions below however i am not capturing every instance because I actually have more than one format unfortunatelly. here are all the formats i have to account to:

 2x5x6 
 2"x5"6"
 2'x5'6'
 2"x5'x6
 1/2"x5/8"x7'

So every digit can be round number or it can be like 1/2, and every digit can occur as ether inch or foot. is is possible to write this in one line or i have to create few functions to accommodate for each?

This should help.

import re

s = "STACKED STONE 52X36X72 AREAWELL BOMAN KEMP"
m = re.search(r"\b(\d+X\d+X\d+)\b", s, flags=re.I)
if m:
    print(m.group(1))

Output:

52X36X72

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM