6

How to match the following i want all the names with in the single quotes

This hasn't been much that much of a twist and turn's to 'Tom','Harry' and u know who..yes its 'rock'

How to extract the name within the single quotes only

name = re.compile(r'^\'+\w+\'')
Rajeev
  • 44,985
  • 76
  • 186
  • 285

4 Answers4

10

The following regex finds all single words enclosed in quotes:

In [6]: re.findall(r"'(\w+)'", s)
Out[6]: ['Tom', 'Harry', 'rock']

Here:

  • the ' matches a single quote;
  • the \w+ matches one or more word characters;
  • the ' matches a single quote;
  • the parentheses form a capture group: they define the part of the match that gets returned by findall().

If you only wish to find words that start with a capital letter, the regex can be modified like so:

In [7]: re.findall(r"'([A-Z]\w*)'", s)
Out[7]: ['Tom', 'Harry']
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    As names usually have more than one or two characters i additionally propose to use (\w{3:}) instead of (\w+), which means "three or more". – marue Apr 03 '12 at 11:23
3

I'd suggest

r = re.compile(r"\B'\w+'\B")
apos = r.findall("This hasn't been much that much of a twist and turn's to 'Tom','Harry' and u know who..yes its 'rock'")

Result:

>>> apos
["'Tom'", "'Harry'", "'rock'"]

The "negative word boundaries" (\B) prevent matches like the 'n' in words like Rock'n'Roll.

Explanation:

\B  # make sure that we're not at a word boundary
'   # match a quote
\w+ # match one or more alphanumeric characters
'   # match a quote
\B  # make sure that we're not at a word boundary
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

^ ('hat' or 'caret', among other names) in regex means "start of the string" (or, given particular options, "start of a line"), which you don't care about. Omitting it makes your regex work fine:

>>> re.findall(r'\'+\w+\'', s)
["'Tom'", "'Harry'", "'rock'"]

The regexes others have suggested might be better for what you're trying to achieve, this is the minimal change to fix your problem.

lvc
  • 34,233
  • 10
  • 73
  • 98
  • @burhan that too, yes; I'll update the answer to include it. In my experience, 'hat' is more common but I can agree to disagree with Wikipedia. :-) – lvc Apr 03 '12 at 13:09
-1

Your regex can only match a pattern following the start of the string. Try something like: r"'([^']*)'"

Marcin
  • 48,559
  • 18
  • 128
  • 201