首页 \ 问答 \ 使用gsub去除多个字符(Using gsub to strip multiple characters)

使用gsub去除多个字符(Using gsub to strip multiple characters)

我有一个对象,其中包含以下字符串"XXXXyyyy!!!!zzzz"

XXXX - 永远不变,永远改变,可能是一百万字符长
yyyy - 永远不会改变,确切的字符数
!!!! - 这是我想要在清理字符串后得到的数据。 此数据也将始终更改
zzzz - 永远不会改变,确切的字符数

因此,如果:

string = "XXXXyyyy!!!!zzzz"  
string.gsub("zzzz","")  
# => "XXXXyyyy!!!!"

接下来我该怎么办才能得到"!!!!" 。 如何使用'yyyy' (静态)擦除"XXXXyyyy"并仅留下"!!!!" 。 我希望这能解释我的问题。 抱歉最后一个。


I have an object which contains the following string "XXXXyyyy!!!!zzzz"

XXXX - never the same, will always change, it may be a million characters long
yyyy - will never change, exact number of characters all the time
!!!! - this is the data I want to result with after I clean the string. This data will always change as well
zzzz - will never change, exact number of characters all the time

so if:

string = "XXXXyyyy!!!!zzzz"  
string.gsub("zzzz","")  
# => "XXXXyyyy!!!!"

What can I do next to get just "!!!!". How can I use 'yyyy', which is static, to erase "XXXXyyyy" and leave only "!!!!". I hope this explains my question. Sorry about the last one.


原文:https://stackoverflow.com/questions/5932406
更新时间:2019-07-08 12:55

最满意答案

你应该做一次:

string[/yyyy(.*)zzzz\z/, 1]  

You should do it in once:

string[/yyyy(.*)zzzz\z/, 1]  
2011-05-09

相关问答

更多

在两个字符之间使用gsub作为数字(Using gsub for a number between two characters)

awk解决方案。 这将做到: $ awk -F_ -v OFS=_ '{print $1,$2,$4}' file HNRNPF-human_SRA:SRR4421749_ENCFF938GRX.fastq.gz RBFOX2-human_SRA:SRR4421654_ENCFF187PBG.fastq.gz U2AF2-human_SRA:SRR3469570_ENCFF158ZML.fastq.gz HNRNPK-human_SRA:SRR3469488_ENCFF267TVR.fastq.g ...

使用AWK gsub()删除重复的字符(Remove duplicate characters using AWK gsub())

$ awk -v RS='!' '{gsub(/^[[:space:]]*/, ""); gsub(/[[:space:]]+/, " ")} NF{print $0 RS}' file hello ! hello you! hello world! hello universe ! 怎么运行的 -v RS='!' 这会将记录分隔符设置为感叹号。 gsub(/^[[:space:]]*/, "") 这将删除所有前导空格。 [[:space:]]是一种匹配任何空白区域的unicode安全方式,包括 ...

从UTF-8国际字符中删除重音符的Ruby方法(Ruby method to remove accents from UTF-8 international characters)

我一般用I18n来处理这个: 1.9.3p392 :001 > require "i18n" => true 1.9.3p392 :002 > I18n.transliterate("Hé les mecs!") => "He les mecs!" I generally use I18n to handle this: 1.9.3p392 :001 > require "i18n" => true 1.9.3p392 :002 > I18n.transliterate("Hé les m ...

如何将多个正则表达式放入gsub?(How to put multiple regular expressions into gsub?)

\p{L}将匹配任何字母, \s将匹配任何空格,制表符或换行符。 如果我们用^否定这个,我们可以用""替换所有不匹配: gsub("[^\\p{L}\\s]+", "", textToClean, perl = TRUE) "polish letters ł Ł ż Ż ą Ą ę Ę ó Ó " 请注意,这些数字也会被删除。 如果要保留数字,可以使用\p{P} , 它只匹配interpunction: gsub("\\p{P}", "", textToClean, perl = TRUE ...

多次使用负向预测(或使用^匹配多个字符)?(Using negative lookahead multiple times (or matching multiple characters with ^)?)

你很近 /<script[^>]*>(?:(?!<\/script>).)*<\/script>/g 你必须有东西吃实际的脚本主体。 那就是这个. 在这里做。 先行检查必须在每个字符之前进行,因此它被包含在一个额外的(非捕获)组中。 要捕获组1中的脚本源代码,只需在评论中指出的@AlanMoore (?:...)周围添加另一组parens。 You were close /<script[^>]*>(?:(?!<\/script>).)*<\/script>/g You must have s ...

R - 仅用一个替换多个字符的出现次数。(R - Replace Multiple Ocurrences of character by only one . REGEX)

你可以用 string <- "Actividades Financieras && Bancarias #### 23" gsub("([#&\\s])\\1+", "\\1", string, perl=TRUE) # => [1] "Actividades Financieras & Bancarias # 23" 查看正则表达式演示和在线R演示 。 细节 ([#&\\s]) - 捕获与# , &或空格匹配的组1 \\1+ - 对第1组值的反向引用,将其匹配1次或更多次(由于+量词)

正则表达式去除特殊字符(Regex to Strip Special Characters)

您的正则表达式包含一个空格,所以在调用string.Replace之前,空格会被去掉。 string regie = @"([{}\(\)\^$&._%#!@=<>:;,~`'\’ \*\?\/\+\|\[\\\\]|\]|\-)"; ^ here 从正则表达式中删除它,你的代码应该做你期望的: string regie = @"([{}\(\)\^$&._%#!@=<>:;,~`'\’\*\?\/\+\ ...

每隔> n个字符替换子字符串(有条件地插入空格的换行符)(Replace substring every >n characters (conditionally insert linebreaks for spaces))

您可以使用.{21,}?\s正则表达式匹配任何21(自nchar > 20 )字符或更多字符,但尽可能少,直到最近的空格: > gsub("(.{21,}?)\\s", "\\1\n", mystring) [1] "this string is annoyingly\nlong and therefore I would\nlike to insert linebreaks" 细节 : (.{21,}?) - 第1组捕获任何21个或更多的字符,但尽可能少(因为{21,}?是一个懒惰的量词)

相关文章

更多

最新问答

更多
  • URL中此字符串格式的重要性是什么?(What's the significance of this string format in URLs?)
  • 如何创建矢量图形,然后从中生成图标?(How to create vector graphics and then generate icons from them?)
  • 动态Jasper报告列(Dynamic Jasper Report Columns)
  • 如何替换LogoList中的子列表(how to replace sublist in LogoList)
  • 从SQL Server导出到Excel收到错误(Exporting to Excel from SQL Server getting error)
  • 遍历python中的对象(Traversing objects in python)
  • 使用Python消除文件中的冗余(Eliminate redundancies from a file using Python)
  • 用于编译导入的类的命令(Command to compile imported classes)
  • PHP:preg_replace使用反向引用替换缩写(PHP: preg_replace replace abbreviation using backreferences)
  • Gradle Eclipse获取编译问题(Gradle Eclipse getting compile issues)
  • 如何在windows phone 8.1 / Windows 8.1中为http客户端请求操作设置超时(How to set time out for http client request operation in windows phone 8.1/Windows 8.1)
  • 用于从移动应用程序的mysql服务器获取数据的PHP代码[关闭](PHP code to fetch data from mysql server for mobile app [closed])
  • TF30063:您无权访问 - Visual Studio Online(TF30063: You are not authorized to access - Visual Studio Online)
  • 以aspnetBoilerplate中的所有租户的主机管理员身份获取记录(Get records as host admin for all tenants in AspnetBoilerplate)
  • 通过UWSGI / NGINX将参数传递给Python Flask(Pass parameters to Python Flask via UWSGI / NGINX)
  • Vim不会将备份文件保存到正确的目录中(Vim won't save backup files into the correct directory)
  • Intellij无法运行Scala代码(Intellij not able to run Scala Code)
  • opengl的依赖关系(opengl dependencies)
  • 确定具有不同优先级的两个布局约束中的哪一个确定它们影响的维/锚的值(Determining which of two layout constraints with different priorities is determining the value of the dimension/anchor they affect)
  • 在Java中是否可以将对象完全转换为其基类型?(Is it possible in Java to turn an object fully into its base type?)
  • 如果陈述不起作用?(If statements not working?)
  • 如何重定向shell脚本输出从python脚本运行[重复](How to redirect shell script output ran from a python script [duplicate])
  • 重置后尝试访问指针(Trying to access pointer after resetting)
  • Postgres优化插入/读取(Postgres optimize for insert/read only)
  • 360观察者统一,纹理在顶部和底部出现翘曲(360 viewer in unity, texture appears warped in the top and bottom)
  • .net Rx:消息的有序批处理(.net Rx: in-order batch-processing of messages)
  • 查询在给定区域中包含全景图的商业场所的api?(Query places api for business venues in given area which contain panoramas?)
  • iOS7和jQuery mobile:面板在打开和关闭后变为空白(iOS7 and jQuery mobile: panel goes blank once opened and closed)
  • 从Geany内部运行应用程序的多个实例(Run multiple instances of an application from inside Geany)
  • 使用Yii2在邮件中嵌入图像时出错(Error when embed image in mail using Yii2)