首页 \ 问答 \ 使用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?(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 ...

Ruby-gsub br标记为\ n \ n用于API,但包括任何空格(Ruby - gsub br tags to \n\n for API, but including any whitespace)

在搞乱HTML或XML时,最好使用解析器。 我先从: require 'nokogiri' doc = Nokogiri::HTML::DocumentFragment.parse(<<EOT) <p>this<br>is<br> some <br> text<br>and<br >some<br/>more</p> EOT doc.search('br').each { |br| br.replace("\n\n") } doc.to_html # => "<p>this\n" + # ...

从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 ...

R - gsub在同一行代码中替换和清理(R - gsub replace and clean in same line of code)

这是一个单行: gsub(pattern = "[A-z ,/]", replacement = "", x = string) # added / to address the update 要么 gsub(pattern = "[A-z]| |,|/", replacement = "", x = string) 正如hwnd所指出的,范围[Az]实际上包括几个特殊字符,它们位于Ascii代码中的AZ和az之间( 相关的SO答案和ASCII表 )。 这些特殊字符是: [ , \ , ] , ...

使用as.character将表达式强制转换为字符时,是否有办法避开500个字符的限制?(Is there a way to get around the 500 character limit when coercing expressions to characters using as.character?)

你可以用deparse和gsub做丑陋的事情: expr1 <- parse(text = text1) attributes(expr1) <- NULL (text3 <- paste(deparse(expr1), collapse="")) #rm whitespace etc (text3 <- gsub("\\)$", "", gsub("^expression\\(", "", gsub("[[:space:]][[:space:]]+", " ", text3)) ...

clojure等同于ruby的gsub(clojure equivalent for ruby's gsub)

您可以使用Java的replaceAll方法。 该电话将如下所示: (.replaceAll "text" "(\\d)([ap]m|oclock)\\b" "$1 $2") 请注意,这将返回一个新的字符串(像gsub (没有爆炸)在红宝石)。 gsub!没有等价物gsub! 在Clojure中Java / Clojure字符串是不可变的。 You can use Java's replaceAll method. The call would look like: (.replaceAll " ...

python替换字符串中的多个字符(python replace multiple characters in a string)

至于替换字符串中的多个字符 你可以使用str.maketrans和str.translate : >>> char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" >>> secretkey = "Dd18Abz2EqNPWhYTOjBvtVlpXaH6msFUICg4o0KZwJeryQx3f9kSinRu5L7cGM" >>> trans = str.maketrans(char, secretkey) ...

为什么str.strip()比str.strip('')快得多?(Why is str.strip() so much faster than str.strip(' ')?)

在时尚博士中: 这是因为在两种不同情况下存在两种功能,可以在unicode_strip看到; do_strip和_PyUnicodeXStrip第一次执行比第二次快得多。 函数do_strip适用于str.strip(arg)被调用的情况,即提供了参数的情况下,不存在任何参数的常见情况str.strip()以及do_argstrip (它包装_PyUnicode_XStrip )。 do_argstrip只是检查分隔符,如果它是有效的并且不等于None (在这种情况下它调用do_strip ),它 ...

相关文章

更多

最新问答

更多
  • Android宽度:100%修复(网站接管问题)(Android width:100% fix (website takeover issue))
  • C ++函数/方法设计的良好实践(Good practice in C++ function/method design)
  • 计算其他表中不存在的所有记录 - SQL查询(Count all records that does not exist to other table - SQL Query)
  • 为什么我要用JPA共享ID?(Why do I get shared Ids with JPA?)
  • asp.net - 如何显示来自html格式的数据行的字段(asp.net - how to display a field from data row that is in html format)
  • 我们如何使用ActiveRecord从连接表中删除行?(How can we delete rows from a join table by using ActiveRecord?)
  • ng-class搞乱了类的顺序(ng-class messing with the order of classes)
  • oracle 12g无效数字错误(oracle 12g invalid number error)
  • 更改ng-src值onclick(Change ng-src value onclick)
  • 如何在android中自动添加自定义依赖项以创建新项目?(How to add custom dependencies automatically in android for ever a new project is created?)
  • datetime函数在PHP中(datetime function in php)
  • 在javascript中获取会话数组的值(in javascript get the value of a session array)
  • 如何在UTF8中编译LaTeX?(How can I compile LaTeX in UTF8? [closed])
  • Rspec:“array.should == another_array”,但不用担心订单(Rspec: “array.should == another_array” but without concern for order)
  • Logcat错误:无法在android片段中加载视图(Logcat error: unable to load view in android fragments)
  • JavaFX的。(JavaFX. Adding items to the list in different threads is not working)
  • 从GDATA日历资源迁移到Google Calendar Resource api(Migrate from GDATA calendar resource to Google Calendar Resource api)
  • SSRS 2008 - 以零情景处理分割(SSRS 2008 - Dealing with division by zero scenarios)
  • 我如何以编程方式添加一个listView列标题的点击事件(How can I add a listView column header a click event programmatically)
  • Wxpython:无法检索有关列表控件项XXX的信息(Wxpython: Couldn't retrieve information about list control item XXX)
  • 使用Tortoise SVN在SVN存储库中移动目录(Move Directory across SVN repository using Tortoise SVN)
  • 天蓝色服务结构集群中的web api无状态服务是否在一段时间不活动后进入休眠状态?(Do web api stateless services in azure service fabric cluster go to sleep after a period of inactivity?)
  • 我可以设置intelliJ来突出显示PHP编码风格吗?(Can I set intelliJ to highlight php coding style?)
  • 用javafx创建一个Truetype字体文件(Creating a Truetype Font file with javafx)
  • Spring ftp配置错误(Spring ftp configuration is wrong)
  • 使用gsub去除多个字符(Using gsub to strip multiple characters)
  • 续订推送证书并保持当前的App Store App正常工作(Renew Push certificate and keep current App Store App working)
  • js:ES5和ES6之间关于'this'关键字用法的一个令人困惑的观点(js: one confusing point about 'this' keyword usage between ES5 and ES6)
  • window.onload vs $(document).ready()(window.onload vs $(document).ready())
  • 在Swift中,如何声明一个符合一个或多个协议的特定类型的变量?(In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?)