newbie: emacs 'split-string' 
Author Message
 newbie: emacs 'split-string'

Hi folks,

I have a problem with the emacs lisp 'split-string' function.

Let's say I want to split the string "abc" at every match of the
regular expression "a+".  The expression (split-string "bab" "a+")
evaluates to ("b" "b"), which is fine.

On the other hand, (split-string "abc" "a+") evaluates to ("bc")
instead of to ("" "bc").  This means, there is no leading empty
string, despite the leading match. So in a sense, 'split-string' does
not really 'split'. We loose information about the matches at the
beginning (and at the end).

In which way can I obtain the desired behavior?  Surrounding the
string with a leading and trailing character does not work, since it
could interfere with the regexp match. E.g., if we choose
"a" as the leading/trailing character (some definite character must be
 chosen), evaluating (split-string "aabca" "a+" ) still yields ("bc").
Note that this example shows that 'split-string' does
MATCH the regexp at the beginning of a string. It just does not insert
a "" into the result list.

I'm not claiming there is anything wrong with 'split-string', I simply
need a different function.

Any help is appreciated.

Cheers, Mirko.

--
Dr. Mirko Luedde



Tue, 10 Sep 2002 03:00:00 GMT  
 newbie: emacs 'split-string'

Quote:
> Hi folks,

> I have a problem with the emacs lisp 'split-string' function.

> Let's say I want to split the string "abc" at every match of the
> regular expression "a+".  The expression (split-string "bab" "a+")
> evaluates to ("b" "b"), which is fine.

> On the other hand, (split-string "abc" "a+") evaluates to ("bc")
> instead of to ("" "bc").  This means, there is no leading empty
> string, despite the leading match. So in a sense, 'split-string' does
> not really 'split'. We loose information about the matches at the
> beginning (and at the end).

> In which way can I obtain the desired behavior?  

Sure, add the leading and trailing empty strings by hand,
conditionally.  Pseudocode:

        Do the usual split-string

        (when (string-match (concat "^" regex) string)
        add a leading ""
        )

        (when (string-match (concat regex "$") string)
        add a trailing ""
        )

You'll want to build the regular expressions a little more carefully
in case they already begin/end with bol/eol, but I'm sure you get the
idea.

Quote:

> I'm not claiming there is anything wrong with 'split-string', I simply
> need a different function.

--
Tom Breton, http://world.std.com/~tob
Not using "gh" since 1997. http://world.std.com/~tob/ugh-free.html
Rethink some Lisp features, http://world.std.com/~tob/rethink-lisp/index.html


Tue, 10 Sep 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Splitting a string every 'n'

2. Newbie question - input doesn't work in Emacs python-mode

3. STRING 'make'/'remake'

4. Getting Ascii string from hex 'string'

5. 'split' creates extra output

6. Splitting 'and' conditions into multiple conditions

7. splitting using '\' as a delimiter

8. Behavior of 'split' command

9. delimiter '::' with split

10. String#split(' ') and whitespace (perl user's surprise)

11. How to split a string (newbie Q)

12. string.split and re.split inconsistency

 

 
Powered by phpBB® Forum Software