是否可以在gsub表达式中使用否定匹配?我希望替换字符串,hello 除了那些开头的字符串hello Peter
my-string.gsub(/^hello@/i, '')
Run Code Online (Sandbox Code Playgroud)
我该@怎么做而不是?
我有很长的字符串列表,例如这个机器可读的例子:
A <- list(c("Biology","Cell Biology","Art","Humanities, Multidisciplinary; Psychology, Experimental","Astronomy & Astrophysics; Physics, Particles & Fields","Economics; Mathematics, Interdisciplinary Applications; Social Sciences, Mathematical Methods","Geriatrics & Gerontology","Gerontology","Management","Operations Research & Management Science","Computer Science, Artificial Intelligence; Computer Science, Information Systems; Engineering, Electrical & Electronic","Economics; Mathematics, Interdisciplinary Applications; Social Sciences, Mathematical Methods; Statistics & Probability"))
Run Code Online (Sandbox Code Playgroud)
所以它看起来像这样:
> A
[[1]]
[1] "Biology"
[2] "Cell Biology"
[3] "Art"
[4] "Humanities, Multidisciplinary; Psychology, Experimental"
[5] "Astronomy & Astrophysics; Physics, Particles & Fields"
[6] "Economics; Mathematics, Interdisciplinary Applications; Social Sciences, Mathematical …Run Code Online (Sandbox Code Playgroud) 如何仅使用1 gsub删除多个空格和尾随空格?我已经完成了这个功能trim <- function(x) gsub(' {2,}',' ',gsub('^ *| *$','',x)),但我只想用1 gsub重写它.
实际上,我希望精益如何匹配基于gsub之后/之前的内容.在这个例子中,我需要匹配前面有一个空格的所有空格,并用''替换它们
我在下面的代码中得到了一个例外.有人可以告诉我我做错了什么,以及如何防止它?
def self.find_by_data(data = {})
where(name_canonical: data['name'].downcase.gsub(/\s+/, ''),
fuel: data['fuel'],
trim_canonical: data['trim'].downcase.gsub(/\s+/, ''),
year: data['year']).first
end
Run Code Online (Sandbox Code Playgroud)
例外:
/Users/charlie/Documents/WIP/projectx/ar_models.rb:35:in `find_by_data': undefined method `downcase' for nil:NilClass (NoMethodError)ooooooooooooooooooooooooo| ETA: 0:00:00
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:36:in `block in find_by_data'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/collection_proxy.rb:845:in `block in scoping'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/relation.rb:270:in `scoping'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/collection_proxy.rb:845:in `scoping'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:36:in `find_by_data'
from /Users/charlie/Documents/WIP/projectx/ar_models.rb:132:in `create_or_assign_existing'
from /Users/charlie/Documents/WIP/projectx/app.rb:230:in `block (2 levels) in work'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
from /Users/charlie/Documents/WIP/projectx/app.rb:80:in `block in work'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:319:in `call'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:319:in `call_with_index'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:179:in `block (3 levels) in work_in_threads'
from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:326:in `with_instrumentation' …Run Code Online (Sandbox Code Playgroud) 我有一个包含一些名字的向量.我想在每一行中提取标题,基本上是","(包括空格)和"."之间的所有内容.
> head(combi$Name)
[1] "Braund, Mr. Owen Harris"
[2] "Cumings, Mrs. John Bradley (Florence Briggs Thayer)"
[3] "Heikkinen, Miss. Laina"
[4] "Futrelle, Mrs. Jacques Heath (Lily May Peel)"
[5] "Allen, Mr. William Henry"
[6] "Moran, Mr. James"
Run Code Online (Sandbox Code Playgroud)
我想gsub可能会有用但我很难找到合适的正则表达式来满足我的需求.
如何更换空字符串?
这个:
x = c("","b")
gsub("","taco",x)
Run Code Online (Sandbox Code Playgroud)
生产:
"taco" "tacobtaco"
Run Code Online (Sandbox Code Playgroud)
代替:
"taco" "b"
Run Code Online (Sandbox Code Playgroud)
有没有办法替换空字符串?
我有两个字符串 - 每个字符串有很多行,如下所示:
value_1 = "DEFAULT-VLAN"
value_2 = "WAN"
data = "HOSTNAME = DEFAULT-VLAN"
result = string.gsub(data,value_1,value_2)
print(result)
Run Code Online (Sandbox Code Playgroud)
结果:
data = "HOSTNAME = DEFAULT-VLAN"
Run Code Online (Sandbox Code Playgroud)
当连字符(" - ")从它正在工作的值中删除时.有没有简单的方法来解决这个问题?
谢谢!
我想\n在R中的一个非常长的字符向量中用换行符(\n )替换空格.但是,我不想替换每个空格,但仅当子字符串强制执行一定数量的字符(n)时.
例:
mystring <- "this string is annoyingly long and therefore I would like to insert linebreaks"
Run Code Online (Sandbox Code Playgroud)
现在我想在mystring每个子字符串长度大于20个字符(nchar > 20)的条件下在每个空格中插入换行符.
因此,结果字符串应该如下所示:
"this string is annoyingly\nlong and therefore I would\nlike to insert linebreaks")
Run Code Online (Sandbox Code Playgroud)
\n在25,26和25个字符后插入了换行符(\n ).
我怎样才能做到这一点?也许东西结合gsub和strsplit?
我需要\"从矢量中删除.这是我的数据:
data <- c("\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1803224&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-linux-security-masterclass-3-in-1%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1848638&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fmastering-kali-linux%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1426684&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Finformation-gathering-with-kali-linux%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1628300&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-switchblade%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1615700&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fadministrador-de-sistemas-junior-en-windows-server-y-linux%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.809770&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flearn-bash-shell-in-linux-for-beginners-lite%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.574388&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fhow-to-install-linux-ubuntu-server%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1436610&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fcentos-and-ubuntu-managing-packages%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1771266&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-foundation-certified-system-administrator-exam%2F",
"\"https://click.linksynergy.com/link?id=RUxZriH*PWc&offerid=323058.1734052&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Flinux-server-security%2F"
)
Run Code Online (Sandbox Code Playgroud)
如您所见,每个对象都以\".我怎样才能专门删除这些字符并留下链接?