regex - Remove everything else from the String using regular expression -
i have regular expression allowed in string.
^\pl*['#-]?\pl*\$
the requirements have changed , need drop else string. example 1 of these special characters ', #, , - allowed.
how can change regex drop else not fit ?
here list of expected values:
johno'connell -> allowed. should is. johndias -> allowed. should is. johnoconnell' -> allowed. should is. johnoconnell# -> allowed. should is. johnoconnell- -> allowed. should is. johnoconnell-# -> should return johnoconnell- johnoconn34ell -> should return johnoconnell *johnoconnell -> should return johnoconnell johnoconnell* -> should return johnoconnell johnoconn$%ell -> should return johnoconnell
thanks
if understand correctly, way:
// test data def tests = [ [ input:"johno'connell", output:"johno'connell" ], [ input:"johndias", output:"johndias" ], [ input:"johnoconnell'", output:"johnoconnell'" ], [ input:"johnoconnell#", output:"johnoconnell#" ], [ input:"johnoconnell-", output:"johnoconnell-" ], [ input:"johnoconnell-#", output:"johnoconnell-" ], [ input:"johnoconn34ell", output:"johnoconnell" ], [ input:"*johnoconnell", output:"johnoconnell" ], [ input:"johnoconnell*", output:"johnoconnell" ], [ input:"johnoconn\$%ell", output:"johnoconnell" ] ] string normalize( string input ) { int idx = 0 input.replaceall( /[^a-za-z'#\-]/, '' ) // remove disallowed chars .replaceall( /['#\-]/ ) { match -> // replace 2nd+ instance of special chars idx++ == 0 ? match : '' } } tests.each { assert normalize( it.input ) == it.output }
Comments
Post a Comment