所以我似乎无法弄清楚这一点......我有一个字符串说,"a\\nb"我希望这成为"a\nb".我已经尝试了以下所有,似乎没有工作;
>>> a
'a\\nb'
>>> a.replace("\\","\")
File "<stdin>", line 1
a.replace("\\","\")
^
SyntaxError: EOL while scanning string literal
>>> a.replace("\\",r"\")
File "<stdin>", line 1
a.replace("\\",r"\")
^
SyntaxError: EOL while scanning string literal
>>> a.replace("\\",r"\\")
'a\\\\nb'
>>> a.replace("\\","\\")
'a\\nb'
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么最后一个有效,因为这很好用:
>>> a.replace("\\","%")
'a%nb'
Run Code Online (Sandbox Code Playgroud)
这里有什么我想念的吗?
编辑我明白\是一个转义字符.我在这里要做的是将所有\\n \\t等等转换成\n \t等等,并且替换似乎没有像我想象的那样工作.
>>> a = "a\\nb"
>>> b = "a\nb"
>>> print a
a\nb
>>> print b
a
b
>>> a.replace("\\","\\")
'a\\nb'
>>> a.replace("\\\\","\\")
'a\\nb'
Run Code Online (Sandbox Code Playgroud)
我希望字符串a看起来像字符串b.但是替换不是像我想的那样替换斜线.
您好我试图从字符串中删除所有特定字符.我一直在使用String.Replace,但它没有,我不知道为什么.这是我目前的代码.
public string color;
public string Gamertag2;
private void imcbxColor_SelectedIndexChanged(object sender, EventArgs e)
{
uint num;
XboxManager manager = new XboxManagerClass();
XboxConsole console = manager.OpenConsole(cbxConsole.Text);
byte[] Gamertag = new byte[32];
console.DebugTarget.GetMemory(0x8394a25c, 32, Gamertag, out num);
Gamertag2 = Encoding.ASCII.GetString(Gamertag);
if (Gamertag2.Contains("^"))
{
Gamertag2.Replace("^" + 1, "");
}
color = "^" + imcbxColor.SelectedIndex.ToString() + Gamertag2;
byte[] gtColor = Encoding.ASCII.GetBytes(color);
Array.Resize<byte>(ref gtColor, gtColor.Length + 1);
console.DebugTarget.SetMemory(0x8394a25c, (uint)gtColor.Length, gtColor, out num);
}
Run Code Online (Sandbox Code Playgroud)
它基本上从我的Xbox 360中检索字符串的字节值,然后将其转换为字符串形式.但我希望它删除"^"的所有实例String.Replace似乎不起作用.它什么都没做.它只是留下以前的字符串.任何人都可以向我解释它为什么这样做?
我想删除字符串中的所有URL(用""替换它们)我搜索过但却找不到我想要的东西.
例:
text1
text2
http://url.com/bla1/blah1/
text3
text4
http://url.com/bla2/blah2/
text5
text6
http://url.com/bla3/blah3/
Run Code Online (Sandbox Code Playgroud)
我希望结果如下:
text1
text2
text3
text4
text5
text6
Run Code Online (Sandbox Code Playgroud) 如何从String中删除双引号?
例如:我希望"abd生产abd,没有双引号.
这是我尝试过的代码:
line1 = line1.replaceAll("\"(\\b[^\"]+|\\s+)?\"(\\b[^\"]+\\b)?\"([^\"]+\\b|\\s+)?\"","\"$1$2$3\"");
Run Code Online (Sandbox Code Playgroud) 是否有一个更短的方式来编写以下代码?
my_string = my_string.replace('A', '1')
my_string = my_string.replace('B', '2')
my_string = my_string.replace('C', '3')
my_string = my_string.replace('D', '4')
my_string = my_string.replace('E', '5')
Run Code Online (Sandbox Code Playgroud)
请注意,我不需要替换那些确切的值; 我只是想找到一种方法将5+行变成少于5行
我需要找出一个正则表达式来删除所有不以"+"或" - "开头的行.
我想打印一个大型差异文件的纸质副本,但它在实际差异之前和之后显示5行左右.
string.sub看起来它只替换了第一个实例.是否有可以替换所有模式的方法或其他方法?你能在像perl这样的正则表达式中做到吗?
(我觉得像r/blah/blah /)
...和任何能告诉我为什么在地球上确实string.sub取代第一场比赛的人?
我在查找如何用'和'替换字符串中的最后一个','时遇到问题:
有这个字符串:test1,test2,test3
我希望以:test1,test2和test3结束
我正在尝试这样的事情:
var dialog = 'test1, test2, test3';
dialog = dialog.replace(new RegExp(', /g').lastIndex, ' and ');
Run Code Online (Sandbox Code Playgroud)
但它不起作用
我正在尝试使用正则表达式来仅擦除字符串的匹配部分.我正在使用该preg_replace函数,并试图通过在匹配部分周围加上括号来删除匹配的文本.例:
preg_replace('/text1(text2)text3/is','',$html);
Run Code Online (Sandbox Code Playgroud)
这会用''代替整个字符串.我只想删除text2,但保留text1和text3.如何匹配和替换匹配的字符串部分?
我想替换String输入中的一些字符串:
string=string.replace("<h1>","<big><big><big><b>");
string=string.replace("</h1>","</b></big></big></big>");
string=string.replace("<h2>","<big><big>");
string=string.replace("</h2>","</big></big>");
string=string.replace("<h3>","<big>");
string=string.replace("</h3>","</big>");
string=string.replace("<h4>","<b>");
string=string.replace("</h4>","</b>");
string=string.replace("<h5>","<small><b>");
string=string.replace("</h5>","</b><small>");
string=string.replace("<h6>","<small>");
string=string.replace("</h6>","</small>");
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,这种方法并不是最好的,因为每次我都要搜索要替换的部分等,而且字符串是不可变的......输入也很大,这意味着需要考虑一些性能问题.
有没有更好的方法来降低此代码的复杂性?
replace ×10
string ×6
regex ×5
python ×3
java ×2
c# ×1
char ×1
double ×1
javascript ×1
php ×1
preg-match ×1
python-2.7 ×1
regex-group ×1
ruby ×1
sed ×1
slash ×1
url ×1
vim ×1
winforms ×1