我试图在案文中将"o“替换为”·“:
·指导该部的技术工作 ·作为项目负责人履行监督和管理职责 确定方向以确保目标和目的 O选择管理人员和其他关键人员 与执行同事合作,制定和执行公司计划和部门战略。 监督部门年度财务计划和预算的编制和执行 O管理绩优薪酬 ·履行分配的其他职责
因为这些都是我尝试过的
test<- sub(test, pattern = "o ", replacement = "• ") # does not work
test<- gsub(test, pattern = "^o ", replacement = "• ") # does not work
test<- gsub(test, pattern = "o ", replacement = "• ") # works but it also replaces to to t•为什么"^o“不能工作,因为它只出现在每一行的开头。
发布于 2022-11-14 18:03:12
这些都是一个单一的值吗?如果是这样的话,请使用查找后的方法在换行符或字符串开始之后找到o:
test2 <- gsub(test, pattern = "(?<=\n|\r|^)o ", replacement = "• ", perl = TRUE)
cat(test2)• Direct the Department’s technical
• Perform supervisory and managerial responsibilities as leader of the program
• Set direction to ensure goals and objectives
• Select management and other key personnel
• Collaborate with executive colleagues to develop and execute corporate initiatives and department strategy
• Oversee the preparation and execution of department’s Annual Financial Plan and budget
• Manage merit pay
• Perform other duties as assigned或者,将每一行的值拆分为单独的值,然后使用原始regex:
test3 <- gsub(unlist(strsplit(test, "\n|\r")), pattern = "^o ", replacement = "• ")
test3 [1] "• Direct the Department’s technical"
[2] ""
[3] "• Perform supervisory and managerial responsibilities as leader of the program"
[4] ""
[5] "• Set direction to ensure goals and objectives"
[6] ""
[7] "• Select management and other key personnel"
[8] ""
[9] "• Collaborate with executive colleagues to develop and execute corporate initiatives and department strategy"
[10] ""
[11] "• Oversee the preparation and execution of department’s Annual Financial Plan and budget"
[12] ""
[13] "• Manage merit pay"
[14] ""
[15] "• Perform other duties as assigned" 发布于 2022-11-14 19:43:34
这里不需要任何查找,请使用^和(?m)标志:
test <- gsub(test, pattern = "(?m)^o ", replacement = "• ", perl=TRUE)(?m)重新定义了^锚点的行为,如果您指定了m标志,这意味着“行的开始”。
test <- "• Direct the Department’s technical\n\no Set direction to ensure goals and objectives\n\no Select management and other key personnel"
cat(gsub(test, pattern = "(?m)^o ", replacement = "• ", perl=TRUE))输出:
• Direct the Department’s technical
• Set direction to ensure goals and objectives
• Select management and other key personnelhttps://stackoverflow.com/questions/74435902
复制相似问题