-7

I have a set of strings like this:

C001F01.PNG

C001G01.PNG

C002F10.PNG

which follow the format of :

C(id number)(F or G)(another id number).PNG

I want to know their ids' and know if they were from class F or G, I have read that re.split() could do similar work, but I'm confused and don't understand how RE works exactly.

Community
  • 1
  • 1
Farhood
  • 361
  • 2
  • 3
  • 16

2 Answers2

1

You should certainly read more on regex. A first hint is that when you want to capture a pattern you need to enclose it in parentheses. e.g. (\d+). For this example though, the code you need is:

match = re.match(r'C(\d+)([F|G])(\d+)\.PNG', s)

first_id = match.group(1)
fg_class = match.group(2)
second_id = match.group(3)
dvitsios
  • 430
  • 2
  • 7
  • thanks a lot. I've tried to read this(https://docs.python.org/2/library/re.html), but couldn't understand it much. – Farhood Jul 25 '17 at 10:54
0
s = "123STRINGabcabc"

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""



print find_between( s, "123", "abc" )
Gaurav Srivastava
  • 3,129
  • 3
  • 14
  • 34
Sharath
  • 1
  • 1
  • Welcome to StackOverflow. Please see [How do I format my code blocks?](//meta.stackexchange.com/q/22186) – Tushar Jul 25 '17 at 11:10