标签: gsub

如果需要使用gsub如何添加尾部斜杠

如果需要,我正在尝试添加尾部斜杠:

a = '/var/www'
a.gsub
...
Run Code Online (Sandbox Code Playgroud)

我不知道怎么做.

ruby gsub

6
推荐指数
3
解决办法
5642
查看次数

在正则表达式中使用负面条件

是否可以在gsub表达式中使用否定匹配?我希望替换字符串,hello 除了那些开头的字符串hello Peter

my-string.gsub(/^hello@/i, '')
Run Code Online (Sandbox Code Playgroud)

我该@怎么做而不是?

ruby regex gsub

6
推荐指数
1
解决办法
4035
查看次数

在R中正确使用gsub /正则表达式?

我有很长的字符串列表,例如这个机器可读的例子:

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)

regex r list gsub

6
推荐指数
2
解决办法
494
查看次数

使用gsub删除多个空格和尾随空格

如何仅使用1 gsub删除多个空格和尾随空格?我已经完成了这个功能trim <- function(x) gsub(' {2,}',' ',gsub('^ *| *$','',x)),但我只想用1 gsub重写它.

实际上,我希望精益如何匹配基于gsub之后/之前的内容.在这个例子中,我需要匹配前面有一个空格的所有空格,并用''替换它们

regex r gsub

6
推荐指数
1
解决办法
4898
查看次数

Ruby Undefined方法downcase

我在下面的代码中得到了一个例外.有人可以告诉我我做错了什么,以及如何防止它?

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)

ruby exception gsub

6
推荐指数
1
解决办法
1万
查看次数

提取字符串中两个符号之间的所有内容

我有一个包含一些名字的向量.我想在每一行中提取标题,基本上是","(包括空格)和"."之间的所有内容.

> 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可能会有用但我很难找到合适的正则表达式来满足我的需求.

regex r gsub

6
推荐指数
1
解决办法
774
查看次数

如何在R中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)

有没有办法替换空字符串?

r gsub

6
推荐指数
3
解决办法
9531
查看次数

Lua string.gsub用连字符

我有两个字符串 - 每个字符串有很多行,如下所示:

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)

当连字符(" - ")从它正在工作的值中删除时.有没有简单的方法来解决这个问题?

谢谢!

string lua hyphen gsub

6
推荐指数
2
解决办法
3462
查看次数

每隔> n个字符替换子字符串(有条件地插入空格的换行符)

我想\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 ).

我怎样才能做到这一点?也许东西结合gsubstrsplit

regex r gsub

6
推荐指数
1
解决办法
763
查看次数

从字符串中删除双引号\"符号

我需要\"从矢量中删除.这是我的数据:

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)

如您所见,每个对象都以\".我怎样才能专门删除这些字符并留下链接?

regex r gsub stringr

6
推荐指数
3
解决办法
2321
查看次数

标签 统计

gsub ×10

r ×6

regex ×6

ruby ×3

exception ×1

hyphen ×1

list ×1

lua ×1

string ×1

stringr ×1