Python: returns a string between // regex

I still don't understand the regex. I read the docs, but after I came up with the regex character, how to use them?

For example, I want to return only what was between the first two lines

en/lemon_peel/n/, ca/llimona/n/, 

the output for is should be: lemon_peel and llimona

I tried in the regex test something like this: ([^/]*) but never worked

+1
source share
2 answers

If you want to use the regular expression, you should use the regular expression as follows:

 import re tmp_string = ''' en/lemon_peel/n/, ca/llimona/n/, ''' reg = re.search('/([^/,]*)/',tmp_string) result = reg.group(1) 

In your case, you will find all characters that are not '/'.

Or you can use the split function instead of re :

 tmp_string.split('/')[1] 
+2
source

If you absolutely want to use regex, you can use the following:

 import re pattern = r'\w[^/]*' results = re.findall(pattern, "en/lemon_peel/n/") 

The value of results is a list:

 ['en', 'lemon_peel', 'n'] 
+1
source

Source: https://habr.com/ru/post/1396207/


All Articles