str_detect 的帮助页面指出“相当于 grepl(pattern, x)”,但是:
str_detect("ALL-", str_c("\\b", "ALL-", "\\b"))
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
尽管
grepl(str_c("\\b", "ALL-", "\\b"), "ALL-")
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
我想其中之一没有按预期工作?或者我错过了什么?
我使用了很多str_replace,例如:
$prune = str_replace("'", '\'', $prune);
$prune = str_replace('’', '\'', $prune);
$prune = str_replace('–', '-', $prune);
$prune = str_replace('“', '', $prune);
$prune = str_replace('"', '', $prune);
$prune = str_replace('é', 'e', $prune);
$prune = str_replace('&', '&', $prune);
Run Code Online (Sandbox Code Playgroud)
是否有更简单的PHP函数,例如htmlspecialchars_decode()?
在Emacs中,如何删除文件中的所有空行(包括制表符和空格)?
可以M-x replace-regexp做到的伎俩?
我可以用regexp找到空行:^ [st]*$,但不知道如何通过删除来替换它.
谢谢!
$srting = "test1 test1 test2 test2 test2 test1 test1 test2";
Run Code Online (Sandbox Code Playgroud)
如何更改test1值test2和test2值test1?
当我使用时str_replace,preg_replace所有值都更改为最后一个数组值.例:
$pat = array();
$pat[0] = "/test1/";
$pat[1] = "/test2/";
$rep = array();
$rep[0] = "test2";
$rep[1] = "test1";
$replace = preg_replace($pat,$rep,$srting) ;
Run Code Online (Sandbox Code Playgroud)
结果:
test1 test1 test1 test1 test1 test1 test1 test1
Run Code Online (Sandbox Code Playgroud) 我有弦乐"a,b,c,d,,,,, ",",,,,a,,,,"
我希望这些字符串分别转换为"a,b,c,d"和",,,,a".
我正在为此写一个正则表达式.我的java代码看起来像这样
public class TestRegx{
public static void main(String[] arg){
String text = ",,,a,,,";
System.out.println("Before " +text);
text = text.replaceAll("[^a-zA-Z0-9]","");
System.out.println("After " +text);
}}
Run Code Online (Sandbox Code Playgroud)
但这是删除所有逗号.
如何写这个来实现上面给出的?
我正在使用 R 中的 Twitter API 提取推文。我一直在使用 write.csv2 命令将结果保存到 r 中的 csv 中,这很好,但存在一个问题,即推文文本中的字符返回导致多行一条推文的电子表格。
我尝试过使用 str_replace_all 但它似乎对我不起作用,而且我找不到任何原因。
这是我的代码
searchTags = c("Galwaybikeshare", "Corkbikeshare", "dublinbikes", "BelfastBikes", "SantanderCycles", "CitiBikeNYC", "obike", "Hubway", "bicing")
additionalParams = c("-rt -http")
searchString <- paste((paste(searchTags[1:9], collapse = " OR ")), additionalParams, collapse = "")
tweets_list <- searchTwitter(searchString, n=20, lang = "en", resultType = 'recent')
str_replace_all(tweets_list, "[\r\n]" , "")
tweets.df <- twListToDF(tweets_list)
todayDate <- Sys.Date()
tweetArchive <- paste("BikeShareTweets ", todayDate, ".csv", sep ="")
write.csv2(tweets.df, file = tweetArchive)
Run Code Online (Sandbox Code Playgroud)
下面的文本是导致该问题的推文示例。
"TransitNinja205: 0.01% of the …Run Code Online (Sandbox Code Playgroud) 我有一个Python字符串,我想\include在一个乳胶文件中.但是,我的字符串有一个下划线字符(_),\_在包含之前需要将其转换为(这是_的乳胶表示).
如果我做:
self.name = self.laser_comboBox.currentText() #Get name from a qt widget
latex_name = self.name.replace("_", r"\_") #Replacing _ for \_
print self.name
print latex_name
Run Code Online (Sandbox Code Playgroud)
我去拿:
XXXXXX-YY\_SN2017060009
XXXXXX-YY\_SN2017060009
Run Code Online (Sandbox Code Playgroud)
可以看出,两个变量都被替换了.这意味着该replace方法就位.但是,如果我这样做:
self.name = self.laser_comboBox.currentText() #Get name from a qt widget
latex_name = str(self.name).replace("_", r"\_") #Converting to str and then replacing _ for \_
print self.name
print latex_name
Run Code Online (Sandbox Code Playgroud)
我去拿:
XXXXXX-YY_SN2017060009
XXXXXX-YY\_SN2017060009
Run Code Online (Sandbox Code Playgroud)
这让我很困惑......我想知道为什么会这样......
是否有类似于str.replace的方法可以执行以下操作:
>> replace(sequence=[0,1,3], old=[0,1], new=[1,2])
[1,2,3]
Run Code Online (Sandbox Code Playgroud)
它应该像str.replace一样:用另一个序列替换序列的"片段",而不是用"new"的元素映射"old"的元素.谢谢 :)
我正在尝试使用 str_replace_all 将许多不同的值(即“Mod”、“M2”、“M3”、“Interviewer”)替换为一个一致的字符串(即“Moderator:”)。我正在对多个不同的类别进行此操作,并且我希望避免将每个唯一值都写出来,因为有很多。
因此,我制作了一个包含所有我想要标准化的唯一值的小标题并读取它,然后拉出每列(为了简单起见,只有 5 个,但只显示了 2 个)以将它们变成向量:
speak_names <- read_csv("speak_names.csv")
speak_namesMisc <- dplyr::pull(speak_names, Misc)
speak_namesMod <- dplyr::pull(speak_names, Moderator)
Run Code Online (Sandbox Code Playgroud)
对于替换值,我制作了一个与上述向量等长的字符向量,因为我知道替换和模式必须是等长的:
Misc <- rep("Misc:", 2)
Mod <- rep("Moderator:", 28)
Run Code Online (Sandbox Code Playgroud)
当我使用此代码运行 Misc 时,它工作得很好:
atas_clean$speaker <- str_replace_all(atas_clean$speaker, speak_namesMisc, Misc)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试相同的版主版本时(即使我尝试在 Misc 之前运行它),我收到一条错误消息:
atas_clean$speaker <- str_replace_all(atas_clean$speaker, speak_namesMod,
Mod)
Warning message:
In stri_replace_all_regex(string, pattern, fix_replacement(replacement), :
longer object length is not a multiple of shorter object length
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我会收到这个错误,因为这个相同的函数产生 TRUE:
identical(length(speak_namesMod), length(Mod))
Run Code Online (Sandbox Code Playgroud)
如果这对模式或替换有任何影响,我正在使用的数据框的长度为 16,244 行。我被困住了,试图找出为什么这不起作用和/或另一个不涉及在向量中输入每个字符元素的解决方案。
谢谢!
这是我的任务
journey = """Just a small tone girl
Leaving in a lonely whirl
She took the midnight tray going anywhere
Just a seedy boy
Bored and raised in South Detroit or something
He took the midnight tray going anywhere"""
Run Code Online (Sandbox Code Playgroud)
总的。好的,对于这个练习,你的工作是使用 Python 的字符串替换方法来修复这个字符串并将新版本打印到控制台。
这就是我所做的
journey = """ just a small tone girl
Leaving in a lonely whirl
she took a midnight tray going anywhere
Just a seedy boy
bored and raised in south detroit or something
He took the midnight tray …Run Code Online (Sandbox Code Playgroud) str-replace ×10
string ×5
python ×3
r ×3
regex ×3
php ×2
character ×1
emacs ×1
java ×1
preg-replace ×1
python-3.x ×1
qt ×1
replace ×1
sequence ×1
stringr ×1
substitution ×1
twitter ×1