Reputation: 313
I want to be able to be able to replace all of the line returns (\n's) in a single string (not an entire file, just one string in the program) with spaces and all commas in the same string with semicolons.
Here is my code:
$str =~ s/"\n"/" "/g;
$str =~ s/","/";"/g;
Upvotes: 2
Views: 8055
Reputation: 70732
This will do it. You don't need to use quotations around them.
$str =~ s/\n/ /g;
$str =~ s/,/;/g;
Explanation of modifier options for the Substitution Operator (s///
)
e Forces Perl to evaluate the replacement pattern as an expression.
g Replaces all occurrences of the pattern in the string.
i Ignores the case of characters in the string.
m Treats the string as multiple lines.
o Compiles the pattern only once.
s Treats the string as a single line.
x Lets you use extended regular expressions.
Upvotes: 10
Reputation: 33380
You don't need to quote in your search and replace, only to represent a space in your first example (or you could just do / /
too).
$str =~ s/\n/" "/g;
$str =~ s/,/;/g;
Upvotes: 3