-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

1

5 Answers 5

2
s = "'FLBC8U', 24"

print re.findall("'([^']*)'", s)[0]
FLBC8U
Sign up to request clarification or add additional context in comments.

Comments

0

My regex:

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

You can check the result here.


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

Comments

0

Try using this pattern:

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

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

Comments

0

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

>>> "('FLBC8U', 24)"[1:-1].split(',')[0]
"'FLBC8U'"

Comments

0

This should solve it for you:

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

Explanation here

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.