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?

thegrinner
  • 11,546
  • 5
  • 41
  • 64
  • sorry input is :"auth-server $1n ame$ [position []] xxxx [match-fqdn [[] [all]]]" – Krishna Jan 29 '14 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. – Tim Pietzcker Jan 29 '14 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. – thegrinner Jan 29 '14 at 16:16
  • do you mean `[` or `]` as opposed to `<` or `>`? – Farmer Joe Jan 29 '14 at 16:16
  • 1
    possible duplicate of [Python: Split string with multiple delimiters](http://stackoverflow.com/questions/4998629/python-split-string-with-multiple-delimiters) – thegrinner Jan 29 '14 at 16:17
  • Thanks for formatting to see properly thats what i meant – Krishna Jan 29 '14 at 16:18
  • You have nested delimiters here. Python doesn't support recursive regexes, that will make things difficult... – Tim Pietzcker Jan 29 '14 at 16:18
  • @farmerjoe , $ can be replaced with < > , there was some problem while entering the question , so i changed to $ , we can always extend to < > once we do for $ – Krishna Jan 29 '14 at 16:24
  • @thegrinner - its similar to what you refereed but the delimiter should also be present especially $ in the list – Krishna Jan 29 '14 at 16:28

5 Answers5

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.

wbest
  • 611
  • 1
  • 6
  • 15
  • 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 ? – Krishna Jan 29 '14 at 16:20
  • 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. – wbest Jan 29 '14 at 16:36
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 [.

Cu3PO42
  • 1,403
  • 1
  • 11
  • 19
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]]]")
BrenoZan
  • 294
  • 1
  • 7
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']]]]]
Guy Gavriely
  • 11,228
  • 6
  • 27
  • 42
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...

Siva Tumma
  • 1,695
  • 12
  • 23