java 6 匹配器类

发布于 2024-09-25 03:58:16 字数 1490 浏览 4 评论 0原文

我有一个字符串如下:

TEST|A||B|C|**""**|D|""|||||\r\n
TEST|Z||V|P|**""**|Y||||||||\r\n

我需要将 | 的第 5 次和第 6 次出现之间的内容设为如果内容为“”则为空白。 所以所需的输出是

TEST|A||B|C||D|""|||||\r\n
TEST|Z||V|P||Y||||||||\r\n

所以我使用 matcher/pattern 和 matcher.replaceFirst() 使用此正则表达式模式:

String regexPattern = "TEST.*\\|([|\\|]*)\\|([|\\|]*)\\|([|\\|]*)\\|([|\\|]*)\\|\"\"\\|([|\\|]*)"

虽然我只想要 | 的第五次和第六次出现中的“”要变为 EMPTY,程序会将遇到的所有“”替换为 EMPTY。

所以输出如下:

TEST|A||B|C||D||||||\r\n
TEST|Z||V|P||Y||||||||\r\n

我不确定是否需要更改我拥有的正则表达式模式?或者有没有办法告诉匹配器类,一旦它更改了第一个“”,就不要更改该匹配模式中的其他“”,而是在 TEST 片段的其他出现中更改它,如果是这样怎么办?

更新:

明白了。对于造成的巨大混乱深感抱歉。我会尽我所能,使其尽可能准确/清晰。

问题改述:

我想使用模式来使用 java Pattern 和匹配器类在字符串中进行搜索。

该字符串有两行,数据由 | 分隔。每行以 \r\n\ 分隔

简化示例如下:

TestString 内容:

TEST|JUNK1|JUNK2|JUNK3|JUNK4|""|JUNK6|""|JUNK8
TEST|JUNK11|JUNK12|JUNK13|JUNK14|""|JUNK16|

我想要 JUNK4| 之后的 ""上例中的(行中第 5 次和第 6 次 | 出现之间的内容)将被设为空/空白。 我不希望该行其他位置的“”发生变化。下一行需要发生同样的事情。

因此,我将以下正则表达式模式与 Java Matcher 和 Pattern 类结合使用,如下所示:

String regexPattern = "TEST.*\\|([|\\|]*)(\"\")([|\\|]*)\\|";
         

但它替换了每行中所有出现的“”,这不符合我的要求。

我尝试了量词/POSIX {1},但徒劳地认为它会限制对 TestStr 中第一个“”的搜索,但徒劳。

具有 {1} 用法的正则表达式:

String regexPattern = "TEST.*\\|([|\\|]*)(\"\"){1}([|\\|]*)\\|";

那么如何在模式中进行限制以获取字符串的片段直到第一个“”。

I have a string as follows:

TEST|A||B|C|**""**|D|""|||||\r\n
TEST|Z||V|P|**""**|Y||||||||\r\n

I need to make the content between 5th and 6th occurence of | to blank if the content is "".
So the desired output is

TEST|A||B|C||D|""|||||\r\n
TEST|Z||V|P||Y||||||||\r\n

So I am using matcher/pattern and matcher.replaceFirst() using this regex pattern:

String regexPattern = "TEST.*\\|([|\\|]*)\\|([|\\|]*)\\|([|\\|]*)\\|([|\\|]*)\\|\"\"\\|([|\\|]*)"

Though I want only the "" in the 5th and 6th occurence of | to be made EMPTY, the progarm is replacing all "" that it encounters to EMPTY.

So the output is as follows:

TEST|A||B|C||D||||||\r\n
TEST|Z||V|P||Y||||||||\r\n

I am not sure if I need to change the regex pattern that I have? OR is there a way to tell the matcher class to tell that once it changes the first "" do not change other "" in that matched pattern but change it in the other occurence of TEST fragment, If so how?

Update:

Got it. Extremely sorry for the mega confusion. I will try my level best to make it precise/clear as much as possible.

Question rephrased:

I want to use a pattern to search in a string using java Pattern and matcher classes.

The string has two lines with data seperated by| and each line separated by \r\n\

Simplified example is as follows:

TestString content:

TEST|JUNK1|JUNK2|JUNK3|JUNK4|""|JUNK6|""|JUNK8
TEST|JUNK11|JUNK12|JUNK13|JUNK14|""|JUNK16|

I want "" which is after JUNK4| (the content between 5th and 6th occurence of | in the line) in the above example to be made empty/blank.
I do not want "" in other positions of that line to change. Same thing needs to happen in the next line.

So I am using the following regex pattern in conjunction to Java Matcher and Pattern classes is as follows:

String regexPattern = "TEST.*\\|([|\\|]*)(\"\")([|\\|]*)\\|";
         

But it is replacing all occurences of "" in each line and this is not as per my requirement.

I tried quantifier/POSIX {1} but in vain thinking that it would restrict the search for the first "" in the TestStr but in vain.

Regex with {1} usage:

String regexPattern = "TEST.*\\|([|\\|]*)(\"\"){1}([|\\|]*)\\|";

So how to make the restriction in the pattern to get the fragment of the string till the first "".

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

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

发布评论

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

评论(1

音盲 2024-10-02 03:58:16

一个不使用复杂正则表达式的简单答案:

String temp[] = inputString.split("\\|");
if ("\"\"".equals(temp[5]))
{
    temp[5] = "";
    StringBuilder buf = new StringBuilder();
    for (String s : temp)
        buf.append("|").append(s);
    inputString = buf.substring(1);
}

另外,请注意其他人对你说的话。使用编辑工具,不要将更正作为答案发布。

A simple answer not using complex regexes:

String temp[] = inputString.split("\\|");
if ("\"\"".equals(temp[5]))
{
    temp[5] = "";
    StringBuilder buf = new StringBuilder();
    for (String s : temp)
        buf.append("|").append(s);
    inputString = buf.substring(1);
}

And also, pay attention to what others are saying to you. USE THE EDIT FACILITY, don't post corrections as answers.

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