From: Christian Neukirchen Date: 2005-03-19T22:19:30+09:00 Subject: Re: Iterating through a string and removing leading characters "Robert Klemme" writes: > "Randy Kramer" schrieb im Newsbeitrag > news:200503181244.16009.rhkramer@gmail.com... >> This is going to seem a little strange (for a number of reasons I might >> mention below), but I would like to iterate through a string from >> beginning >> to end, examining each character, then discarding (or moving) them >> (elsewhere). >> >> This is so that I can, upon finding certain character(s), run one of >> several >> REs on the next part of the string. I'm looking to find a very >> efficient way >> to do this. >> Barring anything better, the approach I may take is to iterate through the >> string and, when I find a potential RE match, use s[n,len] to return a >> partial string to be checked against the RE. > > I'd bet that this approach is slower than a pure regexp based > approach. If you cannot stuck all exact regexps into one (see below) > then maybe some form of stripped regexps might help. For example: > > rx1 = /ab+/ > rx2 = /cd+/ > > rx_all = /(ab+)|(cd+)/ > > rx_stripped = /[ab](\w+)/ > # then, use these on the second part > rx_stripped_1 = /^b+/ > rx_stripped_2 = /^d+/ This reminds me of my old idea for matching against multiple regexp by automatically combining them into larger, alternating regexps thereby reducing the number of matches from n to log2(n)... Anyone willing to code that? To match t against A, B, C, D, E, F, G and H, you first match t against A|B|C|D|E|F|G|H, then against A|B|C|D, then against A|B (or E|F), then against A, C, E or G. Would be of great use for strscan, too. > robert -- Christian Neukirchen http://chneukirchen.org