Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex match text between quotes

Tags:

python

regex

In the following script I would like to pull out text between the double quotes ("). However, the python interpreter is not happy and I can't figure out why...

import re

text = 'Hello, "find.me-_/\\" please help with python regex'
pattern = r'"([A-Za-z0-9_\./\\-]*)"'
m = re.match(pattern, text)

print m.group()

The output should be find.me-/\.

like image 741
syvex Avatar asked Jan 31 '12 19:01

syvex


People also ask

How do you extract text between quotes in Python?

Use the re. findall() method to extract strings between quotes, e.g. my_list = re. findall(r'"([^"]*)"', my_str) .

How to get string between double quotes in python?

To extract strings in between the quotations we can use findall() method from re library.

How do you match double quotes in regex?

Firstly, double quote character is nothing special in regex - it's just another character, so it doesn't need escaping from the perspective of regex. However, because Java uses double quotes to delimit String constants, if you want to create a string in Java with a double quote in it, you must escape them.

What is quotation mark in regex?

the regex looks for a quote mark " then it looks for any possible group of letters thats not " until it finds icon. and any possible group of letters that is not " it then looks for a closing "


1 Answers

match starts searching from the beginning of the text.

Use search instead:

#!/usr/bin/env python

import re

text = 'Hello, "find.me-_/\\" please help with python regex'
pattern = r'"([A-Za-z0-9_\./\\-]*)"'
m = re.search(pattern, text)

print m.group()

match and search return None when they fail to match.

I guess you are getting AttributeError: 'NoneType' object has no attribute 'group' from python: This is because you are assuming you will match without checking the return from re.match.

like image 165
Douglas Leeder Avatar answered Oct 10 '22 00:10

Douglas Leeder