.gsub(/\\..../,"")GSUB。(/ \\(/,"")GSUB(/ \\)/,"")。GSUB(/ \\本地主机/, "")< =这是极少数 [英] .gsub(/\..../, "").gsub(/\(/, "").gsub(/\)/,"").gsub(/\localhost/, "") <= That's a handful

查看:225
本文介绍了.gsub(/\\..../,"")GSUB。(/ \\(/,"")GSUB(/ \\)/,"")。GSUB(/ \\本地主机/, "")< =这是极少数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序,从命令提取信息和格式化输出。

正常命令:

 用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)
用户IP日期(IP)

我的新旅程命令:

 用户
用户
用户
用户
用户
用户
用户
用户
用户
用户
用户

在为了做到这一点,我不得不与 GSUB 完全去除数字和一些其他的东西,整个行看起来是这样的:

 `who`.gsub!(/ \\ D + /,).gsub!(/\\..../,).gsub!(/ \\(/ ,).gsub(/ \\ DEC /,).gsub(/ \\:/,).gsub(/ \\ PTS /,).gsub(/ \\ //,).gsub (/\\./,).gsub(/ \\)/,).gsub(/ \\本地主机/,).gsub(/ \\月/,).squeeze().strip

有没有更好的方式来写这个正则表达式没有这些 GSUB 的?

编辑:

更新从接受的答案输出:把`who`.gsub(/ [\\ t] * /,)

输出现在看起来是这样的:

 约翰
吉姆
杰克
史蒂夫
乔希
埃里克
等等..


解决方案

下面三种方法。首先,请尝试用户命令。 :)

二:

 谁| SED -e'S / [\\ t] * // G'

第三:

 把`who`.gsub(/ [\\ t] * /,)

这三种方法都更短,更简单。

I'm writing a program that will pull the information from a who command and format the output.

Normal who command:

user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)
user ip date (ip)

My new take on who command:

user
user
user
user
user
user
user
user
user
user
user

In order to do this, I had to completely remove the digits with gsub and some other stuff, and the entire line looks like this:

`who`.gsub!(/\d+/, "").gsub!(/\..../, "").gsub!(/\(/, "").gsub(/\Dec/, "").gsub(/\:/, "").gsub(/\pts/, "").gsub(/\//, "").gsub(/\./, "").gsub(/\)/, "").gsub(/\localhost/, "").gsub(/\Nov/, "").squeeze(" ").strip

Is there a better way to write this regex without all these gsub's?

EDIT:

Updated who output from accepted answer: puts `who`.gsub(/[ \t].*/,"")

Output now looks like this:

John
Jim
Jake
Steve
Josh
Eric
etc..

解决方案

Here are three approaches.. First, try the users command. :)

Second:

 who | sed -e 's/[ \t].*//g'

Third:

 puts `who`.gsub(/[ \t].*/,"")

All three are much shorter and much simpler.

这篇关于.gsub(/\\..../,"")GSUB。(/ \\(/,"")GSUB(/ \\)/,"")。GSUB(/ \\本地主机/, "")< =这是极少数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆