如何打印一行的一部分第 2 部分

发布于 2024-12-18 12:53:59 字数 567 浏览 2 评论 0原文

使用 Groovy,我希望抓取制表符分隔行的两个部分。以示例行为例:

onefishtwofishredfishbluefish----(每个字符制表符/t分隔)

假设我想打印one code> 然后我想打印 redfishblue

我该怎么做?

或者,假设我想打印 one,然后打印 red 后面的字符(单词)数量?或者介于两个和蓝色之间?

上一个问题产生了这个响应,用于打印行的某个部分之后的所有内容:

c = ~/.*red(.*)/

m = line =~ c
if (m) {
println m[0][1]
}

产生 fish bluefish 但我对正则表达式的能力不够,无法适当地修改它。我尝试了几次迭代,在其中插入 /t 并修改我的捕获表达式,但我还没有弄清楚。这是三四个问题合而为一,我们将不胜感激。谢谢!!

Using Groovy, I wish to grab two parts of a tab-separated line. Take the example line:

one fish two fish red fish blue fish ----(each character tab /t separated)

Suppose I want to print one and then I want to print red fish blue

How can I do this?

Alternatively, suppose I want to print one and then a count of the number of characters (words) following red? Or between two and blue?

A previous question yielded this response for printing everything following a certain part of the line:

c = ~/.*red(.*)/

m = line =~ c
if (m) {
println m[0][1]
}

to yield fish blue fish but I'm not comptetent enough with regex's to modify this appropriately. I've tried a few iterations, inserting /t in there and modifying my capturing expression but I've not figured it out. This is three or four questions in one, any help is appreciated. Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2024-12-25 12:53:59
def a = [:].withDefault{[]}
def b = [:].withDefault{[]}
def c = 0
def d = 0
def e = 0
def f = 0

seuss = "one\tfish\ttwo\tfish\tred\tfish\tblue\tfish"
a = seuss.split (/\t/)

for (i =0; i<a.size(); i++) {
 if (d != 0) {
    c = c + 1
}
if ( a[i] == "red") {
    d = i
}

}

println a[4] + '\t' + c

for (i =0; i<a.size(); i++) {
if ( a[i] == "blue") {
    e = 0
}
 if (e != 0) {
    f = f + 1
}
if ( a[i] == "two") {
    e = i
}


}

println a[0] + '\t' + f
def a = [:].withDefault{[]}
def b = [:].withDefault{[]}
def c = 0
def d = 0
def e = 0
def f = 0

seuss = "one\tfish\ttwo\tfish\tred\tfish\tblue\tfish"
a = seuss.split (/\t/)

for (i =0; i<a.size(); i++) {
 if (d != 0) {
    c = c + 1
}
if ( a[i] == "red") {
    d = i
}

}

println a[4] + '\t' + c

for (i =0; i<a.size(); i++) {
if ( a[i] == "blue") {
    e = 0
}
 if (e != 0) {
    f = f + 1
}
if ( a[i] == "two") {
    e = i
}


}

println a[0] + '\t' + f
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文