Created
June 19, 2009 11:47
-
-
Save daniello/132572 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(strre). | |
-author("Daniel Kwiecinski: lambder.com"). | |
-purpose("string replace functionality"). | |
-export([sub/3,gsub/3,test/0]). | |
sub(Str,Old,New) -> | |
RegExp = "\\Q"++Old++"\\E", | |
re:replace(Str,RegExp,New,[multiline, {return, list}]). | |
gsub(Str,Old,New) -> | |
RegExp = "\\Q"++Old++"\\E", | |
re:replace(Str,RegExp,New,[global, multiline, {return, list}]). | |
test() -> | |
io:format("~p ~p ~p ~p ~p ~p ~p ~n~p ~n", | |
[ | |
"SELECT * FROM people WHERE first='John' OR last='John'" =:= | |
gsub("SELECT * FROM people WHERE first=$1 OR last=$1","$1","'John'"), | |
"aBc" =:= sub("abc","b","B"), | |
"Abc" =:= sub("abc","a","A"), | |
"abC" =:= sub("abc","c","C"), | |
"aac" =:= gsub("bbc","b","a"), | |
"abc" =:= gsub("abc","d","C"), | |
"abc" =:= sub("abc","d","D"), | |
gsub("SELECT * FROM people WHERE first=$1 OR last=$1","$1","'John'")]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment