Code Comments
Programming Forum and web based access to our favorite programming groups.Improved version. (Remove @ at beginning of lines.)
@# Like split(), but returns array of the matching portions
@# instead of the non-matching ones.
@# Only works if string doesn't contain ASCII 1.
@function splitp(s,A,re, mark,_mark )
@{ delete A
@ mark = sprintf( "%c", 1 )
@ _mark = "[^" mark "]*"
@ if ( gsub( re, mark "&" mark, s ) )
@ gsub( "^" _mark mark "|" mark _mark "$", "", s )
@ else
@ s = ""
@ return split( s, A, mark _mark mark )
@}
@
@BEGIN{
@ test( "foobar", "x" )
@ test( "foobar", "b" )
@ test( "foobar", "^.|.$" )
@ test( " ", " +" )
@ test( " The quick red fox. ", " +" )
@}
@
@function test( s,re ,wheat,chaff )
@{
@ print s "< (Regexp.: " re ")"
@ split( s, wheat, re )
@ splitp( s, chaff, re )
@
@ for (i=1; i in wheat; i++)
@ printf "%s|", wheat[i]
@ print "(wheat)"
@ for (i=1; i in chaff; i++)
@ printf "%s|", chaff[i]
@ print "(chaff)"
@
@ for (i=1; i in wheat; i++)
@ { printf "%s", wheat[i]
@ if (i in chaff)
@ printf "%s", chaff[i]
@ }
@ print "<"
@ print "----------------------------"
@}
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.