string mangling

M

Martin Pirker

Imagine an input string
aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee...

I have regexp for the parts a,b,c
and e can be considered as else.

So how can I efficiently search/step through the string from left to
right, while calling for each section the fitting handler, kind of

case section
/aaaa/ ...

/bbb/ ...

/cccc/ ..

else

end


Thanks for ideas!
Martin
 
R

Robert Klemme

Martin said:
Imagine an input string
aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee...

I have regexp for the parts a,b,c
and e can be considered as else.

So how can I efficiently search/step through the string from left to
right, while calling for each section the fitting handler, kind of

case section
/aaaa/ ...

/bbb/ ...

/cccc/ ..

else

end

You are pretty close:
A
B
C
B
A
B
A
C
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee"

Or, if you want to avoid a second match:
A
B
C
B
A
B
A
C
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee"
Kind regards

robert
 
A

Alex Fenton

Martin said:
Imagine an input string
aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee...

I have regexp for the parts a,b,c
and e can be considered as else.

So how can I efficiently search/step through the string from left to
right, while calling for each section the fitting handler, kind of

You could use String#scan to find bits that find sections that match any of your requirements, then check to see which matched (your patterns could be more complicated, but still distinguishable from one another)

str = 'aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee'

a_rx = /a+/
b_rx = /b+/
c_rx = /c+/

str.scan(/(?:#{a_rx}|#{b_rx}|#{c_rx})/) do | part |
case part
when a_rx
# ...
when b_rx
# ...
when c_rx
# ...
end
end
 
M

Martin Pirker

Robert Klemme said:
Imagine an input string
aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee...

I have regexp for the parts a,b,c
and e can be considered as else.

So how can I efficiently search/step through the string from left to
right, while calling for each section the fitting handler, kind of

case section
/aaaa/ ...

/bbb/ ...

/cccc/ ..

else

end
[...]
Or, if you want to avoid a second match:

of course I want :)
A
B
C
B
A
B
A
C
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee"

....but this doesn't allow a processing step in the "else" case?

Martin
 
J

James Edward Gray II

Imagine an input string
aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee...

I have regexp for the parts a,b,c
and e can be considered as else.

So how can I efficiently search/step through the string from left to
right, while calling for each section the fitting handler, kind of

irb(main):001:0> s = 'aaaaabbccccceeebbbbbbbbbbeaaabaacccceee'
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceee"
irb(main):002:0> s.scan(/(a+)|(b+)|(c+)|([^abc]+)/)
=> [["aaaaa", nil, nil, nil], [nil, "bb", nil, nil], [nil, nil,
"ccccc", nil], [nil, nil, nil, "eee"], [nil, "bbbbbbbbbb", nil, nil],
[nil, nil, nil, "e"], ["aaa", nil, nil, nil], [nil, "b", nil, nil],
["aa", nil, nil, nil], [nil, nil, "cccc", nil], [nil, nil, nil,
"eee"]]
irb(main):003:0>

My similar thought:
=> ["aaaaa", "bb", "ccccc", "eee", "bbbbbbbbbb", "e", "aaa", "b",
"aa", "cccc", "eee"]

James Edward Gray II
 
E

Eero Saynatkari

Martin said:
Imagine an input string
aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee...

I have regexp for the parts a,b,c
and e can be considered as else.

So how can I efficiently search/step through the string from left to
right, while calling for each section the fitting handler, kind of

case section
/aaaa/ ...

/bbb/ ...

/cccc/ ..

else

end

Aside from the oft-mentioned String#scan, you might look into
using StringScanner (require 'strscan') from the stdlib. It is
very good for more complex cases of scanning. Documentation is
available, for example, at http://www.ruby-doc.org/stdlib.
Thanks for ideas!
Martin


E
 
R

Robert Klemme

Martin said:
Robert Klemme said:
Imagine an input string
aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee...

I have regexp for the parts a,b,c
and e can be considered as else.

So how can I efficiently search/step through the string from left to
right, while calling for each section the fitting handler, kind of

case section
/aaaa/ ...

/bbb/ ...

/cccc/ ..

else

end
[...]
Or, if you want to avoid a second match:

of course I want :)
A
B
C
B
A
B
A
C
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee"

...but this doesn't allow a processing step in the "else" case?

You can have an else clause - but it will never be called. I guess you
will process entries between matches. In that case scan won't help - at
least not as used in my example.

A simple option would be to use #split with a group around the whole
regexp and then operate on the array of strings you get. Whether that's
feasible (volume?) in you case I cannot decide.

s.split(/((?:a+)|(?:b+)|(?:c+))/.each do |m|
case section
/aaaa/ ...

/bbb/ ...

/cccc/ ..
else
end
end


Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top