python Regex for mixed alpha numerica data and special characters

Viewed 29

I am trying to write a regex for the following use cases in a one line regex.

ex:

Table 1-2: this is a sample text 2 and some hyphen - (abbreviation)

Table 1: this is a sample text 2 and some hyphen - (abbreviation)

Table 1 this is a sample text 2 and some hyphen - (abbreviation)

Table 1-2-1: this is a sample text 2 and some hyphen - (abbreviation)

similarly

Figure 1-2: this is a sample text 2 and some hyphen - (abbreviation)

Figure 1: this is a sample text 2 and some hyphen - (abbreviation)

Figure 1 this is a sample text 2 and some hyphen - (abbreviation)

Figure 1-2-1: this is a sample text 2 and some hyphen - (abbreviation)

i tried the following approach

import re
re.sub(r'^Table ()|([0-9]+[-][0-9]+|[0-9]+|[0-9 ]+)', " ", text_to_search)
re.sub(r'^Figure ()|([0-9]+[-][0-9]+|[0-9]+|[0-9 ]+)', " ", text_to_search)

Well this is not so good approach, also looking to eliminate the dependency of Table and Figure. Please do suggest. Thanks in advance for your time.

Expected Output:

['Table', '1-2:', 'this is a sample text 2 and some hyphen - (abbreviation)']
['Table', '1:', 'this is a sample text 2 and some hyphen - (abbreviation)']
['Table', '1', 'this is a sample text 2 and some hyphen - (abbreviation)']
['Table', '1-2-1:', 'this is a sample text 2 and some hyphen - (abbreviation)']

['Figure', '1-2:', 'this is a sample text 2 and some hyphen - (abbreviation)']
['Figure', '1:', 'this is a sample text 2 and some hyphen - (abbreviation)']
['Figure', '1', 'this is a sample text 2 and some hyphen - (abbreviation)']
['Figure', '1-2-1:', 'this is a sample text 2 and some hyphen - (abbreviation)']

I am looking for the value available at list[2]

0 Answers
Related