Splitting a string by spaces without space-splitting elements in double-quotes
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
9
down vote
favorite
I need to split a command like:
r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'`
into:
['"C:\Program Files (x86)\myeditor"',
'"$FILEPATH"',
'-n$LINENO',
'"c:\Program Files"',
'-f$FILENAME',
'-aArg2']
That is, I want to split by spaces, but avoid splitting the elements in double-quotes.
I have this code:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
start = end = 0
split =
for elem in re.findall('".*?"', s):
end = s.find(elem)
split.append(s[start:end])
split.append(elem)
start = end + len(elem)
split.extend(s[start:].split())
split = [elem.strip() for elem in split]
split = list(filter(None, split))
It works, but I'm wondering if there's some more elegant/shorter/more readable way to do that in Python(3) ?
python strings python-3.x
add a comment |Â
up vote
9
down vote
favorite
I need to split a command like:
r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'`
into:
['"C:\Program Files (x86)\myeditor"',
'"$FILEPATH"',
'-n$LINENO',
'"c:\Program Files"',
'-f$FILENAME',
'-aArg2']
That is, I want to split by spaces, but avoid splitting the elements in double-quotes.
I have this code:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
start = end = 0
split =
for elem in re.findall('".*?"', s):
end = s.find(elem)
split.append(s[start:end])
split.append(elem)
start = end + len(elem)
split.extend(s[start:].split())
split = [elem.strip() for elem in split]
split = list(filter(None, split))
It works, but I'm wondering if there's some more elegant/shorter/more readable way to do that in Python(3) ?
python strings python-3.x
1
Already answered: stackoverflow.com/q/366202/823470
â tar
Apr 6 at 14:58
2
@tar That answer is on Java and some complex regexes. I think thatshlex
+ copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
â LetMeSOThat4U
Apr 7 at 11:56
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I need to split a command like:
r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'`
into:
['"C:\Program Files (x86)\myeditor"',
'"$FILEPATH"',
'-n$LINENO',
'"c:\Program Files"',
'-f$FILENAME',
'-aArg2']
That is, I want to split by spaces, but avoid splitting the elements in double-quotes.
I have this code:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
start = end = 0
split =
for elem in re.findall('".*?"', s):
end = s.find(elem)
split.append(s[start:end])
split.append(elem)
start = end + len(elem)
split.extend(s[start:].split())
split = [elem.strip() for elem in split]
split = list(filter(None, split))
It works, but I'm wondering if there's some more elegant/shorter/more readable way to do that in Python(3) ?
python strings python-3.x
I need to split a command like:
r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'`
into:
['"C:\Program Files (x86)\myeditor"',
'"$FILEPATH"',
'-n$LINENO',
'"c:\Program Files"',
'-f$FILENAME',
'-aArg2']
That is, I want to split by spaces, but avoid splitting the elements in double-quotes.
I have this code:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
start = end = 0
split =
for elem in re.findall('".*?"', s):
end = s.find(elem)
split.append(s[start:end])
split.append(elem)
start = end + len(elem)
split.extend(s[start:].split())
split = [elem.strip() for elem in split]
split = list(filter(None, split))
It works, but I'm wondering if there's some more elegant/shorter/more readable way to do that in Python(3) ?
python strings python-3.x
asked Apr 6 at 9:22
LetMeSOThat4U
370129
370129
1
Already answered: stackoverflow.com/q/366202/823470
â tar
Apr 6 at 14:58
2
@tar That answer is on Java and some complex regexes. I think thatshlex
+ copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
â LetMeSOThat4U
Apr 7 at 11:56
add a comment |Â
1
Already answered: stackoverflow.com/q/366202/823470
â tar
Apr 6 at 14:58
2
@tar That answer is on Java and some complex regexes. I think thatshlex
+ copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".
â LetMeSOThat4U
Apr 7 at 11:56
1
1
Already answered: stackoverflow.com/q/366202/823470
â tar
Apr 6 at 14:58
Already answered: stackoverflow.com/q/366202/823470
â tar
Apr 6 at 14:58
2
2
@tar That answer is on Java and some complex regexes. I think that
shlex
+ copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".â LetMeSOThat4U
Apr 7 at 11:56
@tar That answer is on Java and some complex regexes. I think that
shlex
+ copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".â LetMeSOThat4U
Apr 7 at 11:56
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
11
down vote
accepted
The best way to do what you want with the standard library would be shlex.split():
>>> import shlex
>>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
>>> shlex.split(s)
['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']
Note that the quotes are not retained.
add a comment |Â
up vote
4
down vote
You could use a different regex:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
pattern = re.compile(r"(("[^"]+")|(-[^s]+))")
for m in re.finditer(pattern, s):
print(m.group(0))
This regex will match either an item enclosed by double quotes ("
) or an item prepended with a dash (-
).
However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
The best way to do what you want with the standard library would be shlex.split():
>>> import shlex
>>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
>>> shlex.split(s)
['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']
Note that the quotes are not retained.
add a comment |Â
up vote
11
down vote
accepted
The best way to do what you want with the standard library would be shlex.split():
>>> import shlex
>>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
>>> shlex.split(s)
['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']
Note that the quotes are not retained.
add a comment |Â
up vote
11
down vote
accepted
up vote
11
down vote
accepted
The best way to do what you want with the standard library would be shlex.split():
>>> import shlex
>>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
>>> shlex.split(s)
['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']
Note that the quotes are not retained.
The best way to do what you want with the standard library would be shlex.split():
>>> import shlex
>>> s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
>>> shlex.split(s)
['C:\Program Files (x86)\myeditor', '$FILEPATH', '-n$LINENO', 'c:\Program Files', '-f$FILENAME', '-aArg2']
Note that the quotes are not retained.
answered Apr 6 at 11:28
etene
2916
2916
add a comment |Â
add a comment |Â
up vote
4
down vote
You could use a different regex:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
pattern = re.compile(r"(("[^"]+")|(-[^s]+))")
for m in re.finditer(pattern, s):
print(m.group(0))
This regex will match either an item enclosed by double quotes ("
) or an item prepended with a dash (-
).
However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.
add a comment |Â
up vote
4
down vote
You could use a different regex:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
pattern = re.compile(r"(("[^"]+")|(-[^s]+))")
for m in re.finditer(pattern, s):
print(m.group(0))
This regex will match either an item enclosed by double quotes ("
) or an item prepended with a dash (-
).
However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
You could use a different regex:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
pattern = re.compile(r"(("[^"]+")|(-[^s]+))")
for m in re.finditer(pattern, s):
print(m.group(0))
This regex will match either an item enclosed by double quotes ("
) or an item prepended with a dash (-
).
However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.
You could use a different regex:
import re
s = r' "C:Program Files (x86)myeditor" "$FILEPATH" -n$LINENO "c:Program Files" -f$FILENAME -aArg2'
pattern = re.compile(r"(("[^"]+")|(-[^s]+))")
for m in re.finditer(pattern, s):
print(m.group(0))
This regex will match either an item enclosed by double quotes ("
) or an item prepended with a dash (-
).
However this might be harder to read/grasp and I'm also not sure if this is considered pythonic as it's the Perl way of doing things so take this with a grain of salt.
edited Apr 6 at 19:11
answered Apr 6 at 10:41
yuri
3,3862832
3,3862832
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191391%2fsplitting-a-string-by-spaces-without-space-splitting-elements-in-double-quotes%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Already answered: stackoverflow.com/q/366202/823470
â tar
Apr 6 at 14:58
2
@tar That answer is on Java and some complex regexes. I think that
shlex
+ copying doublequotes is a more pythonic and sensible approach, as it follows line of thinking "use the standard library if it does the job".â LetMeSOThat4U
Apr 7 at 11:56