使用单词分隔符分割字符串

发布于 2024-11-14 17:01:55 字数 564 浏览 3 评论 0原文

我有一个字符串,

a > b and c < d or d > e and f > g

结果必须是:

a > b
and
c < d
or
d > e
and
f > g

我想在出现“and”、“or”时分割字符串,并检索分隔符以及标记。[我需要它们来评估表达式]

我尝试过使用 StringTokenizer

 new StringTokenizer(x, "\\sand\\s|\\sor\\s", true);

但我没有得到想要的结果。 我尝试使用扫描仪,因为

 Scanner sc = new Scanner(x);
        sc.useDelimiter("and | or");

它可以分割但不返回分隔符。

请建议。

我上面已经给出了 a 、 b 、 c ,但是可能会有单词而不是带有空格的 a、b 、 c 。 更新了示例。

i have a string as below

a > b and c < d or d > e and f > g

outcome must be:

a > b
and
c < d
or
d > e
and
f > g

i want to split the string at occurrences of "and" , "or" and retrieve the delims as well along with the token.[i need them in order to evaluate the expression]

i tried using StringTokenizer as

 new StringTokenizer(x, "\\sand\\s|\\sor\\s", true);

but i dont get desired outcome.
i tried using scanner as

 Scanner sc = new Scanner(x);
        sc.useDelimiter("and | or");

this is able to split but doesnt return the delimiters.

please suggest.

i have given a , b , c above but there cud be words instead of a,b , c with spaces.
Updated example.

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

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

发布评论

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

评论(5

爱格式化 2024-11-21 17:01:55

这将在“and”或“or”上进行拆分,并在单词周围添加任意数量的空格。

   String test = "2 < 3 and 3 > 2 or 4 < 6 and 7 < 8";

    String [] splitString = test.split("\\s*[and|or]+\\s*");
    for(int i = 0; i < splitString.length ; i ++){
        System.out.println(splitString[i]);
    }

输出

2 < 3
3 > 2
4 < 6
7 < 8

This will split on "and" or "or" with any number of spaces surrounding the words.

   String test = "2 < 3 and 3 > 2 or 4 < 6 and 7 < 8";

    String [] splitString = test.split("\\s*[and|or]+\\s*");
    for(int i = 0; i < splitString.length ; i ++){
        System.out.println(splitString[i]);
    }

output

2 < 3
3 > 2
4 < 6
7 < 8
嘿咻 2024-11-21 17:01:55

当您遇到所有不同的空格排列和语法时,您真正想要的是像 JFlex 这样的工具成长。从长远来看,您将节省时间。

What you really want is a tool like JFlex by the time you run into all the different permutations of white spaces and as your syntax grows. In the long run you will save time.

失眠症患者 2024-11-21 17:01:55
String delim = " ";
String[] splitstrings = yourString.split(delim);
for (int i = 0; i < splitstrings.length(); i++) {
    splitstrings += delim;
}
String delim = " ";
String[] splitstrings = yourString.split(delim);
for (int i = 0; i < splitstrings.length(); i++) {
    splitstrings += delim;
}
可可 2024-11-21 17:01:55

据我所知, StringTokenizer 是唯一能够返回所使用的分隔符的 java 标准类。只是从 OT 中复制了正则表达式,假设它会做他想要的事情(第二眼我对他的文字描述非常怀疑,但是哦,好吧 - 只需插入正确的一个)

    String input = "a > b and c < d or d > e and f > g";
    StringTokenizer tokenizer = new StringTokenizer(input, "\\sand\\s|\\sor\\s", true);
    while (tokenizer.hasMoreTokens()) {
        System.out.println(tokenizer.nextToken());
    }

A StringTokenizer is the only java standard class that is capable of returning the used delimiter as well as far as I'm aware. Just copied the regex from the OT assuming that it'll do what he wants (which on second glance I extremely doubt from his textual description, but oh well - just insert the right one)

    String input = "a > b and c < d or d > e and f > g";
    StringTokenizer tokenizer = new StringTokenizer(input, "\\sand\\s|\\sor\\s", true);
    while (tokenizer.hasMoreTokens()) {
        System.out.println(tokenizer.nextToken());
    }
花间憩 2024-11-21 17:01:55
String str = "2 < 3 and 3 > 2 or 4 < 6 and 7 < 8";
System.out.println( ImmutableList.copyOf( str.split( "(?=and|or)" ) ) );

输出:

[2 < 3 , and 3 > 2 , or 4 < 6 , and 7 < 8]
String str = "2 < 3 and 3 > 2 or 4 < 6 and 7 < 8";
System.out.println( ImmutableList.copyOf( str.split( "(?=and|or)" ) ) );

Output:

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