1

I have a stream of structured strings. I want to extract 2 specific sections from each of them in Python language.

For example) my Strings are looking like these:

cpe:2.3:a:troglobit:uftpd:*:*:*:*:*:*:*:*

cpe:2.3:o:google:android:8.0:*:*:*:*:*:*:*

cpe:2.3:a:microsoft:asp.net_core:2.1:*:*:*:*:*:*

I need the BOLD sections only.

Be aware that in the end the number of * is different in each case (it's between 6,7,8). Also, the strings could be started by cpe:2.3:o: or cpe:2.3:a:

I have tried different expressions such as these:

For example, this one works with cases that only have 6 * at the end.

try:
                        temp = re.search("cpe:2.3:a:(.*):\*:\*:\*:\*:\*:\*",testString).group(1)
except AttributeError:
                        temp = 'NOT FOUND'

Or

(Does not work)

try:
                        temp = re.search("(cpe:2.3:a:(.*)|cpe:2.3:o:)(.*)(:\*:\*:\*:\*:\*:\*:\*:\*|:\*:\*:\*:\*:\*:\*:\*|:\*:\*:\*:\*:\*:\*)", testString).group(1)
except AttributeError:
                        temp = 'NOT FOUND'

Or

(Does not work)

 try:    
                        temp = re.search("(cpe:2.3:a:(.*):(.*))':{0}'$", testString).group(1)
 except AttributeError:
                        temp = 'NOT FOUND'

Thank You.

2
  • all lines have cpe:2.3:a: or something like this always? I mean a 3 alphabet character then a : then a number, a dot, another number, another :, another character and another :? if yes check regex101.com/r/JKJ312/1 Commented Jan 23, 2020 at 20:18
  • @HamedGhasempour This works too. thanks Commented Jan 23, 2020 at 21:47

1 Answer 1

1

You can do it without a regex

sample = 'cpe:2.3:a:microsoft:asp.net_core:2.1:*:*:*:*:*:*'

' '.join(sample.split(':')[3:][:2])

'microsoft asp.net_core'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.