Using Regex and string.split

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello there,
i am trying to parse a string expression. The expression looks like this

str = {cc, 1}+{cc, 2}
delimeters = '{', ',', '}'

ant i want to get the result:

cc
1
+
cc
2

Initially i used string.split(str, delimeters) but it returns spaces as
words. I tried Regex but unfortunately i didn't get it to work (or in other
words confused with \#[]#?* etc :)

Any help?

Thanks in advance...

Christos
 
Christos said:
Hello there,
i am trying to parse a string expression. The expression looks like this

str = {cc, 1}+{cc, 2}
delimeters = '{', ',', '}'

ant i want to get the result:

cc
1
+
cc
2

Initially i used string.split(str, delimeters) but it returns spaces as
words. I tried Regex but unfortunately i didn't get it to work (or in other
words confused with \#[]#?* etc :)

Remove all spaces before using Split? (eventually *someone* will pass
something where space is used as a delimiter, but what can you do?)
 
Christos said:
Initially i used string.split(str, delimeters) but it returns spaces as
words. I tried Regex but unfortunately i didn't get it to work (or in other
words confused with \#[]#?* etc :)

Splitting with this regular expression should work fine for a start:

,|\{|\}

As Larry mentions, you might want to get rid of the spaces beforehand,
as well as the line prefix ("str ="). You could use a regex for this :-)

A regex replace of " |str\s*=" (without the quotes) with "" (nothing)
should do it.


Oliver Sturm
 
Back
Top