Wednesday 15 June 2011

regex - escape special character in perl when splitting a string -



regex - escape special character in perl when splitting a string -

i have file in format

string: string1 string: string2 string: string3

i want split lines space , :,so wrote this:

my @array = split(/[:\s]/,$lineoffile);

the result wasn't expected, because within @array split inserts white space , after researches understood have escape \s wrote

my @array = split(/[:\\s]/,$lineoffile);

why have escape \s, character : isn't special character or not?

can explain me that?

thanks in advance.

you don't have double backslash. have tried it?

split /[:\\s]/, $line

will split on colon : or backslash \ or little s s, giving

("", "tring", " ", "tring1")

which isn't want @ all. suggest split on colon followed 0 or more spaces

my @fields = split /:\s*/, $line

which gives result

("string", "string1")

which think want.

regex perl split

No comments:

Post a Comment