i'm PosSible
i'm PosSible

Reputation: 1393

split string according to words between square parenthesis

I want to split string using regex.

for ex.

val = "[python] how to [css]"
val = "[python][css] how to"
val = "how to [python][css]"

my string looks like this(there is try to display different way for value string), and I want to split like:

a=['python','css'] #(type list)
b="how to" #(type string)

I tried this

import re
pat = re.compile(r'(\w+\s*)') 
re.findall(pat,val)  

output:

['python', 'how ', 'to ', 'css']

What am I doing wrong with my regex?

Upvotes: 1

Views: 119

Answers (4)

dragon2fly
dragon2fly

Reputation: 2419

You could try

import re

val = "[python] how to [css]"
m = re.findall(r'\[(\w*)\]', val)
print m
# ['python', 'css']

\[(\w*)\] will match all words inside the square bracket

Upvotes: 1

Vivek Sable
Vivek Sable

Reputation: 10223

Got first part from question a=['python','css'] #(type list)

>>> import re
>>> val = "[python] how to [css]"
>>> [i[1:-1] for i in re.findall("(\[[^\]]*\])", val)]
['python', 'css']
>>> val = "[python][css] how to"
>>> [i[1:-1] for i in re.findall("(\[[^\]]*\])", val)]
['python', 'css']
>>> val = "how to [python][css]"
>>> [i[1:-1] for i in re.findall("(\[[^\]]*\])", val)]
['python', 'css']

second part:(updated according vks solution)

>>> re.sub(r"\[[^\]]*\]","",val) 
'how to '

Upvotes: 1

vks
vks

Reputation: 67968

x="[python] how to [css]"
print re.findall(r"(?<=\[)[^\]]*(?=\])",x)   # this is the list you want
print re.sub(r"\[[^\]]*\]","",x)             # this is the string you want

Try this way.You can have list as well as string.

Upvotes: 2

Maroun
Maroun

Reputation: 95968

The regex (\w+\s*) matches [A-Za-z0-9_] followed by 0 or more spaces, so it'll match css and python in [python] and [css]. This regex: (\w+\s+) matches what you want.

You can do the following:

import re

pat = re.compile(r'\[(.*)\]') 
re.findall(pat,val)  # wil return ['python', 'css']

Now you can get the rest from an opposite regex (matches everything that's not between [ and ]).

Upvotes: 0

Related Questions