Using regular expressions - documentation for further reference
import re
text = 'gfgfdAAA1234ZZZuijjk'
m = re.search('AAA(.+?)ZZZ', text)
if m:
found = m.group(1)
# found: 1234
or:
import re
text = 'gfgfdAAA1234ZZZuijjk'
try:
found = re.search('AAA(.+?)ZZZ', text).group(1)
except AttributeError:
# AAA, ZZZ not found in the original string
found = '' # apply your error handling
# found: 1234
Answer from eumiro on Stack OverflowThe simplest way to extract a substring from a string
how to extract only names in string by using regex
python - Using regex to extract substrings - Stack Overflow
Python: best way to find and extract substring from a string?
Videos
Using regular expressions - documentation for further reference
import re
text = 'gfgfdAAA1234ZZZuijjk'
m = re.search('AAA(.+?)ZZZ', text)
if m:
found = m.group(1)
# found: 1234
or:
import re
text = 'gfgfdAAA1234ZZZuijjk'
try:
found = re.search('AAA(.+?)ZZZ', text).group(1)
except AttributeError:
# AAA, ZZZ not found in the original string
found = '' # apply your error handling
# found: 1234
>>> s = 'gfgfdAAA1234ZZZuijjk'
>>> start = s.find('AAA') + 3
>>> end = s.find('ZZZ', start)
>>> s[start:end]
'1234'
Then you can use regexps with the re module as well, if you want, but that's not necessary in your case.
Hello,
I'm learning python from scratch, and I am trying to figure out how to "extract" a substring from a string. So, for instance, let's say I want to extract the word within the parenthesis for each of these three strings.
'xx(hi)xx' 'abc(there)xyz' 'xx(c)xx'
I know I could use slicing, but this would involve three operations, I think. I'm wondering if there's a sort of pattern I can leverage. How do you guys approach this kind of problem?
Thank you in advance for your help. It is most appreciated!
import re
text = "433 - 675 - 8765 - 322 - 533 - 7665 Mr. Roy Mr. David Mrs. Pooja Ms. Sharma Mr. T "
res=re.findall("",text)
print(res)
Your last "," was beeing matched due to a greedy search, (.*?) is non greedy. Also the last comma is optional so that needs to be ignored if not present
import re
s = r'"url":"a","meta":"b","url":"c"'
print(re.findall(r'("url"):"(.*?)",?', s))
You must escape the , to avoid including the comma inside the group. Try this:
re.findall(r'(("url" :[^,]*),*)', s)
I find regex especially intimidating, so any help here is appreciated. I need to extract a substring of variable length from a longer string. The substring always occurs between two constant patterns, and I don't know how to put that into regex. I'll give an example below:
Below is the big string I need to parse. All I need to extract is "DSC_0026.NEF".
FileMetadata(name='DSC_0026.NEF', id='id:1X3HG93XOX8AAAAAAAMhEA', client_modified=datetime.datetime(2020, 3, 18, 16, 38, 31), server_modified=datetime.datetime(2020, 4, 2, 18, 33, 21), rev='015a253090fa3350000000199c64dd0', size=8015265, path_lower='/02_dunesburyproductimages/2020/03 march/dsc_0026.nef', path_display='/02_DunesburyProductImages/2020/03 March/DSC_0026.NEF', parent_shared_folder_id='6874877392', media_info=None, symlink_info=None, sharing_info=FileSharingInfo(read_only=False, parent_shared_folder_id='6874977392', modified_by='dbid:AAB6MO1TmreSuq_O661AXKiKNAVwJn4N6_4'), is_downloadable=True, export_info=None, property_groups=None, has_explicit_shared_members=None, content_hash='96595111b66a2d47bf6086f74d7c01c2a1738b096737dx9ac0c63ac7becee2c3', file_lock_info=None),
What I think would work is somehow telling the regex to return everything between name=' and ', id, but I have no idea how to do that. I'm not looking for anyone to write code for me; I need to learn regex anyway, so I'd prefer a resource to learn how to do this. The more "dumbed down" it is, the better.
Thanks for any help, I really appreciate it :)