-4

I looked at similar question and answers but could not solve my issue.

I have a string, like the following:

ecc, ecc, .....thisIsUnique("92781227-7e7e-4768-8ee3-4e1615bddf3c", ecc, ecc.......

could be very long before and after without having some unique text.

What I need is to get the 92781227-7e7e-4768-8ee3-4e1615bddf3c code as string. So I'm looking to something that ca sound like:

when you find thisIsUnique go ahead, read the code after you find the first (" characters and keep reading until you find the first ", characters.

Unfortunately I'm not familiar with regex, but maybe there are different ways to solve the problem

thanks to all

matteo
  • 4,683
  • 9
  • 41
  • 77

2 Answers2

2

There are a few sites you should read up on for what regex is. https://regexone.com/ and Learning Regular Expressions Use a site like this to test what you have tried: https://regex101.com/ But to get you started, this runs exactly what you have pasted as an example:

import re

text = 'ecc, ecc, .....thisIsUnique("92781227-7e7e-4768-8ee3-4e1615bddf3c", ecc, ecc.......'
match = re.search('thisIsUnique\("([^"]+)', text)
print (match.group(1))

result:

92781227-7e7e-4768-8ee3-4e1615bddf3c
sniperd
  • 5,124
  • 6
  • 28
  • 44
1

Use re.search:

In [991]: text = 'ecc, ecc, .....thisIsUnique("92781227-7e7e-4768-8ee3-4e1615bddf3c", ecc, ecc.......'

In [992]: re.search('(?<=thisIsUnique\(")(.*?)"', text).group(1)
Out[992]: '92781227-7e7e-4768-8ee3-4e1615bddf3c'

'(?<=thisIsUnique\(")(.*?)"'

Employs a lookbehind.


Additional Reading

  1. Regex HOWTO - getting started with tutorial

  2. General documentation

  3. Additional tutorial site - TutorialsPoint

cs95
  • 379,657
  • 97
  • 704
  • 746
  • @DeepSpace I don't think so? – cs95 Jul 27 '17 at 13:39
  • @matteo for future reference, where could I improve upon my answer? – cs95 Jul 27 '17 at 13:54
  • @COLDSPEED you answer is perfect. I just accept the other for the additional links. Thanks again for the patience! – matteo Jul 27 '17 at 14:25
  • @matteo Oh... is that so. I believe the most important link for you is the regex howto. I have linked you to that in my answer. Online regex editors are fine but at the end of the day they don't teach you how to use regex. – cs95 Jul 27 '17 at 14:28