I need to replace any characters in the set...
[]*?/\:
with an empty string. Can someone post how this is done?
Depends on the language you are using; but you could use (with sed)
sed -e "s/\[\]\*\?\/\\\://g" filename
For C#
Regex.Replace(input, #"[\[\\\]*?/:]", string.Empty)
Perl: $str =~ s/.//g;
But I think you mean replacement with a Space: $str =~ s/./ /g;
For Javascript
var s = "he[ll]o? \*/ foo:bar";
var replaced = s.replace(/[[\\\]*?/:]/g, "");
?replaced
>>hello foobar
Related
I have a text and want to replace all \w\(, for example myword( to the same with a space, so it should be myword (. How to do that with s///? or is there another way to do that?
Try this
$s = "myword( word2(";
$s =~s/(\w+)(\()/$1 $2/g;
print $s;
As from #ikegami command. My above regex \w+ will backtrack this is needless. And no need to group the (, because known one. So i changed my regex accordingly,
New RegEx
$s =~s/(\w)\(/$1 (/g;
Here is a way:
my $str = "myword(";
$str =~ s/(\w+)(\()/$1 $2/;
print $str, "\n";
Output:
myword (
Use look ahead:
$ perl -pe 's/(\w)(?=\()/$1 /' <<< 'word('
word (
Or look ahead together with look behind:
$ perl -pe 's/(?<=\w)(?=\()/ /' <<< 'word('
word (
s/\w\K\(/ (/g # 5.10+
or
s/(\w)\(/$1 (/g
or
s/(?<=\w)\(/ (/g
The first is much faster than the other two, but all are faster than the other correct solutions provided. (Not sure which is the fastest of the second and third.)
Another way will be to use \K i.e forget what you matched before:
#!/usr/bin/perl
use strict;
use warnings;
my $string = q{myword( myword2(};
$string=~s/\w\K\(/ (/g;
print $string,"\n";
How can I delete the characters before "/", including the "/", in a string using Perl or sed?
For instance, this:
ad9a91/FFFF0000
would turn into
FFFF0000
Sed solution
sed 's|[^/]*/||' file
Will remove everything up to and including the first /
or
sed 's|.*/||' file
Will remove everything up to and including the last / .
I added both as the question was not entirely clear on what the format of the string would be every time.
Awk
awk -F/ '{$0=$NF}1' file
This replaces the entire line with whatever is after the last /
You can use substitution,
my $str = "ad9a91/FFFF0000";
$str =~ s|^.+?/||;
or regex capture,
$str = $1 if $str =~ m|/(.+)|s;
Use substitution for this:
my $string = "ad9a91/FFFF0000";
$string =~ s|\.+/||;
my $string = qq(sjdflksdjfsdj ad9a91/FFFF0000 slodjfsdf s);
$string =~ s{\b(\s).*?\b/}{$1}ig;
print $string;exit;
you can also try this split with any space or any tag.
I have a string like this:
$str = "{gfgd}i:123;a:7:{gfgd}i:5;a:35:";
And I want to replace it to:
$str = "{gfgd},{gfgd},";
I want to use ereg_replace with it and replace this kind of phrase:
"i:[0-9]a:[0-9]:" into "," sign.
I try it:
$str = "i:143;a:5:{gfgd}i:123;a:7:{gfgd}i:5;a:35:";
$text = ereg_replace("/^i:[0-9]+;a:[0-9]+:+$", ",", $str);
But i doesn't work. Can you help me?
Thank you in advance
$str = "i:143;a:5:{gfgd}i:123;a:7{gfgd}i:5;a:35";
$str = ereg_replace("\}[^\{]+\{", "},{", $str); // replace between } and { with },{
$str = ereg_replace("^[^\{]+", "", $str); // remove from first
$str = ereg_replace("[^\}]+$", ",", $str); // remove from last
print $str;
Don't use ereg_replace as This function has been DEPRECATED as of PHP 5.3.0
Use preg_replace instead and your regex is wrong. Remove anchors ^ and $
$text = preg_replace('/i:[0-9]+;a:[0-9]+:?/', ",", $str);
//=> ,{gfgd},{gfgd},
Online Demo: http://ideone.com/W2P55n
It looks like you are dealing with a PHP array or object serialized into string. I recommend running:
<?php
$arrayOrObject = unserialize($theEntireStringYouGot);
print_r($arrayOrObject);
?>
That way you may not need to even deal with regex at all.
Note: it will not unserialize a piece of string like in your example, feed it the whole thing.
$string = I am a boy
How to substitute whitespaces between words with underscore ?
You need a regular expression and the substitution operator to do that.
my $string = 'I am a boy';
$string =~ s/\s/_/g;
You can learn more about regex in perlre and perlretut. A nice tool to play around with is Rubular.
Also, your code will not compile. You need to quote your string, and you need to put a semicolon at the end.
$string = 'I am a boy';
$string =~ s/ /_/g;
$string =~ tr( \t)(_); # Double underscore not necessary as per Dave's comment
This is just to show another option in perl. I think Miguel Prz and imbabque showed more smarter ways, personally i follow the way imbabque showed.
my $str = "This is a test string";
$str =~ s/\p{Space}/_/g;
print $str."\n";
and the output is
This_is_a_test_string
If there are more than 2 characters
"Hiiiiiii
My frieeend!!!!!!!"
I need to be reduced to
"Hii
My frieend!!"
Please undestand that in my language there are many words with double chars.
Thnx in advance
kplla
Perl / regex (and if it's not english, Perl has given me better luck with Unicode than PHP):
#!/usr/bin/perl
$str = "Hiiiiii My Frieeeeend!!!!!!!";
$str =~ s/(.)\1\1+/$1$1/g;
print $str;
If a PHP and regex based solution is fine you can do:
$str = "Hiiiiiii My frieeend!!!!!!!";
$str = preg_replace('#(.)\1+#','$1',$str);
echo $str; // prints Hi My friend!
$str = preg_replace('#(.)\1{2,}#','$1$1',$str);
echo $str; // prints Hii My frieend!!
You can make use of the regex used above in Perl too:
$str = "Hiiiiiii My frieeend!!!!!!!";
$str =~s/(.)\1{2,}/$1$1/g;
Here's another regex solution that uses lookahead (just for fun), in Java:
System.out.println(
"Hiiiiii My Frieeeeend!!!!!!!".replaceAll("(.)(?=\\1\\1)", "")
); // prints "Hii My Frieend!!"