ANOOP K
ANOOP K

Reputation: 29

Regular expression to extract pattern form python pandas dataframe column with parenthesis

I have this specific string : Oxidation mark (2-3,20mm- 110mm) , in dataframe column . The text between parenthesis can be anything .

I tried to extract this by the below code:

str.extract('(Oxidation mark )[\(][.]*[\)]')

But its giving NaN .

Upvotes: 0

Views: 286

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627087

You can use

\b(Oxidation\s+mark\s*\([^()]*\))

See the regex demo.

Details

  • \b - a word boundary
  • (Oxidation\s+mark\s*\([^()]*\)) - Group 1:
    • Oxidation - a word
    • \s+ - one or more whitespaces
    • mark - another word
    • \s* - zero or more whitespace chars
    • \([^()]*\) - (, zero or more chars other than ( and ) and then a ) char.

Upvotes: 1

Related Questions