-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,510
  • 26
  • 96
  • 182
  • 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
  • 389,931
  • 88
  • 552
  • 692
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
  • 14,834
  • 4
  • 27
  • 58
0

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

Sven.hig
  • 4,411
  • 2
  • 6
  • 18