删除字符串中小词的出现

编程入门 行业动态 更新时间:2024-10-16 22:15:14
本文介绍了删除字符串中小词的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从字符串中删除特定单词.我无法用简单的全局字符串替换"the"来清空字符串,因为"the"可能是字符串中单词的一部分.

I'm trying to remove a specific word from a string. I can't do a simple global string replace for "the" to empty string as "the" could be part of a word in the string.

word: "the" string: "the_ad_an_feta_cfr_era_the_iop_the" output: "ad_an_feta_cfr_era_iop"

"the"一词可能在字符串的开头,中间或结尾多次,因此我必须考虑分隔符和字符串的开头/结尾.

The word "the" could be at the beginning, several times in the middle or at the end of the string so I have to take into account the separator and beginning/end of string.

我可以用一个正则表达式处理所有这些问题,还是应该求助于循环,但是如何在sed中指定多个模式?

Could I handle all this with one regex or should I resort to looping, but how do I specify the multiple patterns in sed?

sed 's/the//g' <<< "the_ad_feta_cfr_era_the_iop_the"

然后,如果我想从同一字符串中删除几个单词,该怎么办?不仅删除"the",还删除"is","an".所有这些都可以在正则表达式中合而为一吗?

Then how would I do it if I had several words I wanted to remove from the same string? Instead of only "the" also remove "is", "an". Can all this be one in regex without looping?

word: "the", "an", "is" input: "the_ad_an_feta_cfr_era_the_iop_the" output: "ad_feta_cfr_era_iop"

推荐答案

看看这个 sed :

$ string='the_ad_an_feta_cfr_era_the_iop_the' $ sed -E -e ':a' -e 's/(^|_)(the|an|is|feta)(_|$)/\1/g;ta' -e 's/_$//' <<< "$string" ad_cfr_era_iop

请注意,Unix变体之间, sed 的行为有所不同.您的 sed 似乎在标签(或多个 -e 选项)之后需要换行符.进一步阅读:

Note that the behavior of sed differs between Unix variants. Your sed seems to require newlines after labels (or multiple -e options). Further reading:

  • BSD/macOS Sed与GNU Sed与POSIX Sed规范
  • 在Mac OSX上sed与其他标准" sed?

没有标签的版本与 @Cyrus的答案基本相同,但支持带空格的项目":

Version without labels which is essentially the same as @Cyrus' answer but supports "items" with spaces:

$ string='the_ad_an_feta_cfr_era_the cfr_the_iop_the' $ sed -E -e 's/_/__/g;s/(^|_)(the|an|is|feta)(_|$)//g;s/_+/_/g;s/^_//;s/_$//' <<< "$string" ad_cfr_era_the cfr_iop

更多推荐

删除字符串中小词的出现

本文发布于:2023-11-30 07:14:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小词   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!