-1

I have the following string patterns:

A = 'this is an_example-0--1.5-60-NA'

The separator is a hyphen but there are mixed positive/negative numbers. I want to extract the following strings:

string[0] = 'this is an_example'
string[1] = -1.5
string[2] = 60
string[3] = 'NA'

string[1] and string[2] can be either positive or negative. string.split('-') doesn't work here.

scotscotmcc
  • 2,719
  • 1
  • 6
  • 29
Nick
  • 1
  • 1
    There is a `0` in your example. Should it be omitted? – InSync Apr 12 '23 at 00:19
  • Seems like the correct solution is to use some other character as the separator. – John Gordon Apr 12 '23 at 00:51
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Apr 12 '23 at 04:49

1 Answers1

1

You can use a regular expression to split by a '-' not followed by another '-'.

import re

A = 'this is an_example-0--1.5-60-NA'

splitted = re.split(r'(?<!-)-', A)

print(splitted)
#outputs
#['this is an_example', '0', '-1.5', '60', 'NA']
Ignatius Reilly
  • 1,594
  • 2
  • 6
  • 15