首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换多行文本列中的第一行字符

替换多行文本列中的第一行字符
EN

Stack Overflow用户
提问于 2022-11-14 17:53:12
回答 2查看 28关注 0票数 0

我试图在案文中将"o“替换为”·“:

·指导该部的技术工作 ·作为项目负责人履行监督和管理职责 确定方向以确保目标和目的 O选择管理人员和其他关键人员 与执行同事合作,制定和执行公司计划和部门战略。 监督部门年度财务计划和预算的编制和执行 O管理绩优薪酬 ·履行分配的其他职责

因为这些都是我尝试过的

代码语言:javascript
复制
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“不能工作,因为它只出现在每一行的开头。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-14 18:03:12

这些都是一个单一的值吗?如果是这样的话,请使用查找后的方法在换行符或字符串开始之后找到o

代码语言:javascript
复制
test2 <- gsub(test, pattern = "(?<=\n|\r|^)o ", replacement = "• ", perl = TRUE)
cat(test2)
代码语言:javascript
复制
• 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:

代码语言:javascript
复制
test3 <- gsub(unlist(strsplit(test, "\n|\r")), pattern = "^o ", replacement = "• ")
test3
代码语言:javascript
复制
 [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"   
票数 1
EN

Stack Overflow用户

发布于 2022-11-14 19:43:34

这里不需要任何查找,请使用^(?m)标志:

代码语言:javascript
复制
test <- gsub(test, pattern = "(?m)^o ", replacement = "• ", perl=TRUE)

(?m)重新定义了^锚点的行为,如果您指定了m标志,这意味着“行的开始”。

在线R演示

代码语言:javascript
复制
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))

输出:

代码语言:javascript
复制
• Direct the Department’s technical

• Set direction to ensure goals and objectives

• Select management and other key personnel
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74435902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档