-1

I'm currently working on python code to extract a specific strings on a text file between ' '. Here's the code I'm working on:

import re
pattern = "(\w+'*',)"
with open('C:\Documents and Settings\Ash\Desktop\strcount.txt', "r") as text:
    for line in text:
        print re.findall(pattern, line)

and the list of strings in the text file

('FLBC8U', 24)
('cHvQuW', 24)
('7FDm@', 24)
('15ii?', 24)
('H!oDe', 24)
('RB6#U', 24)
('uAmer', 24)
('6NmDJ', 24)
('d-MS1', 24)
('Ejf&B', 24)

I only wanted to take the string in the middle of ' ' single quotation mark before the comma , so the number and the bracket is ignored

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • possible duplicate of [Python Regex to find a string in double quotes within a string](http://stackoverflow.com/questions/9519734/python-regex-to-find-a-string-in-double-quotes-within-a-string) or [Regex for quoted string with escaping quotes](http://stackoverflow.com/questions/249791/regex-for-quoted-string-with-escaping-quotes) – Robin Jun 02 '14 at 09:24

5 Answers5

2
s = "'FLBC8U', 24"

print re.findall("'([^']*)'", s)[0]
FLBC8U
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

This should solve it for you:

(?<=\(')((?!').)*(?=')

Explanation here

Dhrubajyoti Gogoi
  • 1,265
  • 10
  • 18
  • please add some details on why your proposed solution answer the given question. One-line-like answers are likely to be deleted in the future. – furins Jun 02 '14 at 09:40
0

My regex:

(?<=\').*(?=\',)

You can check the result here.


By the way, this site is quite helpful for playing with regex in python.

Ray
  • 2,472
  • 18
  • 22
0

Try using this pattern:

   \(\'(.*)\',.+\)

example: http://regex101.com/r/aZ6tU6

l'L'l
  • 44,951
  • 10
  • 95
  • 146
0

If the strings are simply in a line, you can just do this:

>>> "('FLBC8U', 24)"[1:-1].split(',')[0]
"'FLBC8U'"
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284