-1

String:

text = '<script src="https://mytest.com/abcdefg12-hijklmno34.js"></script>'

I'd like to extract these two alphanumeric values delimited by "-":

regex = '??????'
m = re.split(regex, text)    
print(m)

desired output output:

[
'abcdefg12',
'hijklmno34
]

What's the most elegant regex should I be using for this?

JasonGenX
  • 4,952
  • 27
  • 106
  • 198
  • You could use `r'/([a-z\d.]+)-([a-z\d.]+)(?=[^.])` with the case-indifferent flag set. [ref](https://regex101.com/r/kBnZR9/1/) – Cary Swoveland Jul 15 '20 at 00:35

3 Answers3

2

Try this:

myarray = re.compile('\w+(?=-)|(?<=-)\w+').findall(text)
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0
In [54]: text = '<script src="https://mytest.com/abcdefg12-hijklmno34.js"></script>'

In [55]: m = re.search("com\/(.*)\.", text)

In [56]: m[1]
Out[56]: 'abcdefg12-hijklmno34'

In [57]: m[1].split("-")
Out[57]: ['abcdefg12', 'hijklmno34']
bigbounty
  • 16,526
  • 5
  • 37
  • 65
0

You could try this pattern /(?=\w+\-\w+)(\w+)|(?<=\w\-)(\w+)/g

Sven.hig
  • 4,449
  • 2
  • 8
  • 18