How to replace all characters in a string with '*' in perl - perl5.8

How to get a regular expression to replace all characters in a string in perl with *? The string has some utf-8 or iso-8859-1 characters also. I tried with "s/\w/*/g". But it did not replace utf-8 or iso-8859-1 characters.
my $value="hellö";
print "$value\n";
$value =~ s/\w/*/g;
print "after replacing $value\n"; //It prints ****ö.
I expect all characters should be replaced with * i.e hellö should be replaced with *****.
Please note, few special characters like -,_,\,/ etc should be skipped.

If you want to skip just a few characters, you can always do something along the lines of
s/[^, \/\\\-]/*/g;

To replace all the characters in a string? The \w is for matching word characters, but using just a dot should match all characters: s/./*/g

Related

Replacing non-alphanumeric characters on specific lines in multiple files

I have several (JSON) files in a directory. I am wanting to scan all files for the line with the audio property (first string in quotes) and replace all non-alphanumeric characters in the associated value (second string with quotes) with an underscore. For example, if the line is:
"audio": "Sub Dir/my file` nāme.mp3"
I would like to replace it with:
"audio": "Sub_Dir/my_file__n_me.mp3"
What can I do to replace these lines in multiple files?
If perl is your option, would you please try:
perl -i".bak" -pe 's/("audio":\s"*)(.+?)"/
$x = $1;
($y = $2) =~ s#[^A-Za-z0-9_\/\.]#_#g;
$x . $y . "\""/e' *.json
The e option to the s/../../ function enables the substitution
to be the result of evaluation of perl expression.
The substition s#[^A-Za-z0-9_\/\.]#_#g is performed on the
second string with quotes and replaces characters other
than ascii alphabets, digits, underscores, slashes, dots with underscores.
Original files are saved with the suffix .bak.

adding a colon after every two letters in an alphanumeric string in shell

So i have an alphanumeric string 10006cc2190ab011 i am trying to add a colon after every two letters in this alphanumeric string.
this is the string : 10006cc2190ab011
i want it be - 10:00:6c:c2:19:0a:b0:11
Thanks in advance.
A sed solution:
$ echo 10006cc2190ab011 | sed 's/../&:/g; s/:$//'
10:00:6c:c2:19:0a:b0:11
Replaces each non-overlapping pair of characters with the same pair plus :. In the end removes the trailing : (if input text had even length).
str=10006cc2190ab011; str="${str//??/${.sh.match}:}"; echo ${str%:}
is doing the same replacement without the use of an external command, just using ksh-internals.
Doing the same as in sed (the other answer). Replace in $str every // two charactes ?? with / the matched string and a : (every match is kept in the ksh-variable ${.sh.match}). Then print $str without the last % ':'.

Regex: match something except within arbitrary delimiters

My string:
a = "Please match spaces here <but not here>. Again match here <while ignoring these>"
Using Ruby's regex flavor, I would like to do something like:
a.gsub /regex_pattern/, '_'
And obtain:
"Please_match_spaces_here_<but not here>._Again_match_here_<while ignoring these>"
This should do it:
result = subject.gsub(/\s+(?![^<>]*>)/, '_')
This regex assumes there's nothing tricky like escaped angle brackets. Also be aware that \s matches newlines, TABs and other whitespace characters as well as spaces. That's probably what you want, but you have the option of matching only spaces:
/ +(?![^<>]*>)/
I think, it works:
a = "Please match spaces here <but not here>. Again match here <while ignoring these>"
pattern = /<(?:(?!<).)*>/
a.gsub(pattern, '')
# => "Please match spaces here . Again match here "

How to replace \r in a string in ruby

I have a string that looks like this.
mystring="The Body of a\r\n\t\t\t\tSpider"
I want to replace all the \r, \n, \t etc with a whitespace.
The code I wrote for this is :
mystring.gsub(/\\./, " ")
But this isn't doing anything to the string.
Help.
\r, \n and \t are escape sequences representing carriage return, line feed and tab. Although they are written as two characters, they are interpreted as a single character:
"\r\n\t".codepoints #=> [13, 10, 9]
Because it is such a common requirement, there's a shortcut \s to match all whitespace characters:
mystring.gsub(/\s/, ' ')
#=> "The Body of a Spider"
Or \s+ to match multiple whitespace characters:
mystring.gsub(/\s+/, ' ')
#=> "The Body of a Spider"
/\s/ is equivalent to /[ \t\r\n\f]/
String#tr is designed for stream symbol substitution. It appears to be a bit quickier, than String#gsub:
mystring.tr "\r", ' '
It hasan insplace version also (this will replace all carriage returns, line feed and spaces with space):
mystring.tr! "\s\r\n\t\f", ' '
Stefen's Answer is really very Cool as always comeup with very short and clean solutions. But here what I tried to remove all special characters. [Posted as just optional solution] ;)
> a = "The Body of a\r\n\t\t\t\tSpider"
=> "The Body of a\r\n\t\t\t\tSpider"
> a.gsub(/[^0-9A-Za-z]/, ' ')
=> "The Body of a Spider"
you can use strip , then add a space to your string
mystring.strip . " "
If you literally has \r\n\t in your string:
mystring="The Body of a\r\n\t\t\t\tSpider"
mystring.split(/[\r\t\n]/)

Only an exact match number in a string

How to match (without capturing): 5678
In this string: performer=5678,25678,56342,56782
This is what I have so far: 5678(?!\d)
http://rubular.com/r/WqdK6sQjOK
\b5678\b
... which works also if you delimit your numbers with other separators, such as ;, /, etc

Resources