0

I'm trying to extract a substring with Regex,

Here is my script:

import re
_text = "sdiskpart(device='D:\\', mountpoint='D:\\', fstype='FAT32', opts='rw,fixed')"
print(re.findall("device=(\\'.*\\')", _text))

I'm trying to get the value of device, in this string it's "D:\"

as u can see I tried "device=(\'.*\')" with REgex and it returned:

["'D:\', mountpoint='D:\', fstype='FAT32', opts='rw,fixed'"]

I'm not professional on REgex, How can I force it to take D:\ and print it out ?

1
  • >>> print(re.findall("device=(\\'[A-Z]:\\\\')", _text)) ["'D:\\'"] Commented Jun 22, 2017 at 4:02

2 Answers 2

4

You can use non-eager regexp

import re

print( re.findall("device='(.*?)'", _text))

notice that the .*? means non-eager so it will take the least chars until the next ' ...

Sign up to request clarification or add additional context in comments.

2 Comments

what is the difference in performance compared to using ([^']) (character class of inverse-quote, i.e. all characters but quote) ?
@njzk2 - running timeit resulted in that ".*?" is a bit slower. both does not require backtracking though so I guess they are somewhat in the same order of complexity.
2

REFER https://docs.python.org/3/library/re.html

>>> print(re.findall("device=(\\'[A-Z]:\\\\')", _text))
["'D:\\'"]

You might have to replace * with [A-Z]. I think the drive letter are in caps always else use[A-Za-z]

1 Comment

what if there is no :\\\\ after [A-Z]?

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.