0

I have a following input :

"auth-server $na me$ $1n ame$ [position [$pr io$]] xxxx [match-fqdn [[$fq dn$] [all]]]"

I need to store them in a list with $, <, and > serving as delimiters.

Expected output:

['auth-server', '$na me$', '$1n ame$', '[position', '[$pr io$]]', 'xxxx', '[match-fqdn', '[[$fq dn$]', '[all]]]']

How can I do this?

9
  • sorry input is :"auth-server $1n ame$ [position []] xxxx [match-fqdn [[] [all]]]" Commented Jan 29, 2014 at 16:11
  • 1
    You can always edit your post, that's better than adding comments. Indent a line by four spaces to turn off formatting for that line. Commented Jan 29, 2014 at 16:12
  • I edited your post to format the code for you. If I lost any meaning, don't hesitate to edit your post again to fix it. Commented Jan 29, 2014 at 16:16
  • do you mean [ or ] as opposed to < or >? Commented Jan 29, 2014 at 16:16
  • 1
    possible duplicate of Python: Split string with multiple delimiters Commented Jan 29, 2014 at 16:17

5 Answers 5

3

What you could do is split it on the spaces, then go through each substring and check if it starts with one of the special delimiters. If it does, start a new string and append subsequent strings until you get to the end delimiter. Then remove those substrings and replace them with the new one.

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

2 Comments

What you meant is: words = a.split() ? and then traverse the list again for delimiter ? where a = "auth-server $na me$ $1n ame$ [position [$pr io$]] xxxx [match-fqdn [[$fq dn$] [all]]]" am i right ?
The traversal would be over the "words" list. You'll want to use a second list ("words2") and a temporary string when creating the new specially delimited substring.
1

I think what you want is

import re
re.split(r"(?<=\]) | (?=\$|\[)", "auth-server $na me$ $1n ame$ [position [$pr io$]] xxxx [match-fqdn [[$fq dn$] [all]]]")

This yields

['auth-server', '$na me$', '$1n ame$', '[position', '[$pr io$]]', 'xxxx', '[match-fqdn', '[[$fq dn$]', '[all]]]']

Note however that this is not exactly what you described, but what matches your example. It seems that you want to split on spaces when they are preceded by ] or followed by $ or [.

1 Comment

Thanks i want something like this.
1

try re.split and a regex who make someone cry blood

import re
print re.split(r'(\$[^\$]+\$|\[\S+([^\]]+\]\])?|[-0-9a-zA-Z]+)',"auth-server $na me$ $1n ame$ [position [$pr io$]] xxxx [match-fqdn [[$fq dn$] [all]]]")

Comments

1

consider using pyparsing:

from pyparsing import *
enclosed = Forward()
nestedBrackets = nestedExpr('[', ']') 
enclosed << ( Combine(Group(Optional('$') + Word(alphas) + Optional('$'))) | nestedBrackets )
print enclosed.parseString(data).asList()

output:

[['auth-server', '$na', 'me$', '$1n', 'ame$', ['position', ['$pr', 'io$']], 'xxxx', 
 ['match-fqdn', [['$fq', 'dn$'], ['all']]]]]

Comments

0

Not quite a full answer, but I used regexp search...

a = "auth-server $na me$ $1n ame$ [position [$pr io$]] xxxx [match-fqdn [[$fq dn$] [all]]]"
m = re.search('\$.*\$', a)

combine this with a.split() and we can do the math...

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.