我正在编辑一些来自tesseract ocr的电子邮件.
这是我的代码:
if (email != null) {
email = email.replaceAll(" ", "");
email = email.replaceAll("caneer", "career");
email = email.replaceAll("canaer", "career");
email = email.replaceAll("canear", "career");
email = email.replaceAll("caraer", "career");
email = email.replaceAll("carear", "career");
email = email.replace("|", "l");
email = email.replaceAll("}", "j");
email = email.replaceAll("j3b", "job");
email = email.replaceAll("gmaii.com", "gmail.com");
email = email.replaceAll("hotmaii.com", "hotmail.com");
email = email.replaceAll(".c0m", ".com");
email = email.replaceAll(".coin", ".com");
email = email.replaceAll("consuit", "consult");
}
return email;
Run Code Online (Sandbox Code Playgroud)
但输出不正确.
输入:
amrut=ac.hrworks@g mai|.com
Run Code Online (Sandbox Code Playgroud)
输出:
lalcl.lhlrlwlolrlklsl@lglmlalil|l.lclolml
Run Code Online (Sandbox Code Playgroud)
但是当我在每次替换后将结果分配给新的String时,它工作正常.为什么在同一个String中连续赋值不起作用?
给出以下字符串, "Hi ~+ and ^*. Is ^* still flying around ~+?"
我想替换所有出现"~+",并"^*"以"波比"和"丹尼",因此字符串变成:
"Hi Bobby and Danny. Is Danny still flying around Bobby?"
我宁愿不必两次调用Boost替换函数来替换两个不同值的出现.
我在C++中有一个字符串的问题,它有几个西班牙语单词.这意味着我有很多带有重音符号和波浪号的单词.我想替换他们没有重音的同行.示例:我想替换这个词:哈比亚的"había".我尝试直接替换它,但使用字符串类的替换方法,但我无法让它工作.
我正在使用此代码:
for (it= dictionary.begin(); it != dictionary.end(); it++)
{
strMine=(it->first);
found=toReplace.find_first_of(strMine);
while (found!=std::string::npos)
{
strAux=(it->second);
toReplace.erase(found,strMine.length());
toReplace.insert(found,strAux);
found=toReplace.find_first_of(strMine,found+1);
}
}
Run Code Online (Sandbox Code Playgroud)
dictionary这样的地图在哪里(有更多条目):
dictionary.insert ( std::pair<std::string,std::string>("á","a") );
dictionary.insert ( std::pair<std::string,std::string>("é","e") );
dictionary.insert ( std::pair<std::string,std::string>("í","i") );
dictionary.insert ( std::pair<std::string,std::string>("ó","o") );
dictionary.insert ( std::pair<std::string,std::string>("ú","u") );
dictionary.insert ( std::pair<std::string,std::string>("ñ","n") );
Run Code Online (Sandbox Code Playgroud)
和toReplace字符串是:
std::string toReplace="á-é-í-ó-ú-ñ-á-é-í-ó-ú-ñ";
Run Code Online (Sandbox Code Playgroud)
我显然必须遗漏一些东西.我无法弄清楚.有没有我可以使用的图书馆?
谢谢,
我有这个:
$text = '
hello world
hello
';
Run Code Online (Sandbox Code Playgroud)
仅当它在自己的行上时如何删除?所以在上面的例子中,第二个 应该被删除。结果应该是:
$text = '
hello world
hello
';
Run Code Online (Sandbox Code Playgroud)
通过str_replace(),我可以:
$text = str_replace(' ', '', $text);
Run Code Online (Sandbox Code Playgroud)
但这将删除 的所有实例 ,而不仅仅是当它在自己的行上时。
我想知道Java中是否存在等价于tr ///(在Perl中使用的).例如,如果我想用"密西西比"中的"p"替换所有"s",反之亦然,我可以在Perl中写
#shebang and pragmas snipped...
my $str = "mississippi";
$str =~ tr/sp/ps/; # $str = "mippippissi"
print $str;
Run Code Online (Sandbox Code Playgroud)
我能想到用Java做的唯一方法就是在String.replace()方法中使用虚拟字符,即
String str = "mississippi";
str = str.replace('s', '#'); // # is just a dummy character to make sure
// any original 's' doesn't get switched to a 'p'
// and back to an 's' with the next line of code
// str = "mi##i##ippi"
str = str.replace('p', 's'); // str = "mi##i##issi"
str = str.replace('#', 'p'); // str …Run Code Online (Sandbox Code Playgroud) 我正在寻找最有效(以"最快"的方式)用另一个字符串替换字符串中所有出现的子字符串的方法.到目前为止,我想出的就是:
std::string StringReplaceAll(const std::string &cstSearch, const std::string &cstReplace, const std::string &cstSubject)
{
if(cstSearch.length() > cstSubject.length() || cstSearch == cstReplace || cstSubject.empty() || cstSearch.empty() || cstSubject.find(cstSearch) == std::string::npos)
{
return cstSubject;
}
std::ostringstream ossReturn;
std::string::const_iterator ci(cstSubject.cbegin());
const std::string::const_iterator::difference_type ciFindSize(std::distance(cstSearch.cbegin(), cstSearch.cend()));
for(std::string::const_iterator ciNow; (ciNow = std::search(ci, cstSubject.cend(), cstSearch.cbegin(), cstSearch.cend())) != cstSubject.cend(); ci = ciNow)
{
std::copy(ci, ciNow, std::ostreambuf_iterator<char> (ossReturn));
std::copy(cstReplace.cbegin(), cstReplace.cend(), std::ostreambuf_iterator<char> (ossReturn));
std::advance(ciNow, ciFindSize);
}
std::copy(ci, cstSubject.cend(), std::ostreambuf_iterator<char> (ossReturn));
return ossReturn.str();
}
Run Code Online (Sandbox Code Playgroud)
...这个方式(!!!)对我的需求来说太慢了:-(
期待向你们学习!
我有以下字符串,并希望使用str_replace或preg_replace删除括号但不确定如何.我已经能够使用str_replace删除左括号,但无法删除右括号.
这是刺痛:
$coords = '(51.50972493425563, -0.1323877295303646)';
Run Code Online (Sandbox Code Playgroud)
我试过了:
<?php echo str_replace('(','',$coords); ?>
Run Code Online (Sandbox Code Playgroud)
删除了开头括号,但我现在认为我需要preg_replace来删除它们.
怎么会这样呢?
帮助赞赏
一些背景信息:我有一个古老的基于Web的文档数据库系统,我工作,几乎完全由具有"正常"扩展名(.doc,.xls,.ppt)的MS Office文档组成.它们都是基于某种任意ID号(即1245.doc)命名的.我们正在切换到SharePoint,我需要重命名所有这些文件并将它们分类到文件夹中.我有一个包含各种信息的CSV文件(比如哪个ID号对应于哪个文档的标题),所以我用它来重命名这些文件.我写了一个简短的Python脚本,重命名ID号标题.
但是,文档的某些标题在文件的标题中有斜杠和其他可能不好的字符,所以我想用下划线替换它们:
bad_characters = ["/", "\\", ":", "(", ")", "<", ">", "|", "?", "*"]
for letter in bad_characters:
filename = line[2].replace(letter, "_")
foldername = line[5].replace(letter, "_")
Run Code Online (Sandbox Code Playgroud)
line[2]:"Blah blah boring - meeting 2/19/2008.doc"line[5]:"商务会议2/2008"当我添加循环print letter内部时for,它将打印出它应该替换的字母,但实际上不会像我想要的那样用下划线替换该字符.
我有什么问题吗?
我有字符串变量strVar值为' "value1" ',我想用值替换值中的所有双引号' \" '.所以在更换后的价值看起来像' \"value1\" '
如何在java中执行此操作?请帮助我.
假设有一个字符串"foo boo foo boo"我想用fo替换所有foo和boo的boo.预期的输出是"boo foo boo foo".我得到的是"foo foo foo foo".如何获得预期的输出而不是当前的输出?
$a = "foo boo foo boo";
echo "$a\n";
$b = str_replace(array("foo", "boo"), array("boo", "foo"), $a);
echo "$b\n";
//expected: "boo foo boo foo"
//outputs "foo foo foo foo"
Run Code Online (Sandbox Code Playgroud) str-replace ×10
string ×7
c++ ×3
java ×3
php ×3
boost ×1
performance ×1
preg-replace ×1
python ×1
replace ×1
stl ×1
text ×1
tr ×1