如何在java中的String的split()中使用模式

发布于 2024-12-27 09:31:14 字数 621 浏览 1 评论 0原文

我输入了格式,

    "MG3 4F W 123 TO GH2 2F E 345" 
    or
    "MG3 4F W 123 To GH2 2F E 345"
    or
    "MG3 4F W 123 tO GH2 2F E 345"
    or
    "MG3 4F W 123 to GH2 2F E 345"

我必须根据“TO”分割这种字符串。下面是我尝试使用模式分割它的代码,但它给出了编译错误,你能帮我吗???

    public static final String DIVIDER_PATTERN = "{tT}{oO}";
     public static void main(String[] args) 
     {
        Pattern pt = Pattern.compile(DIVIDER_PATTERN);
        String hello = "Hello to World TO this tO test To how";
        String array[] = hello.split(DIVIDER_PATTERN);
        System.out.println(array[0]);
     }

I have input of the format

    "MG3 4F W 123 TO GH2 2F E 345" 
    or
    "MG3 4F W 123 To GH2 2F E 345"
    or
    "MG3 4F W 123 tO GH2 2F E 345"
    or
    "MG3 4F W 123 to GH2 2F E 345"

i have to split this kind of string based on "TO".Below is the code which i tried to used to split it using pattern but it gives compilation error can you please help me ???

    public static final String DIVIDER_PATTERN = "{tT}{oO}";
     public static void main(String[] args) 
     {
        Pattern pt = Pattern.compile(DIVIDER_PATTERN);
        String hello = "Hello to World TO this tO test To how";
        String array[] = hello.split(DIVIDER_PATTERN);
        System.out.println(array[0]);
     }

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

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

发布评论

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

评论(1

养猫人 2025-01-03 09:31:14

Change {tT}{oO} to [tT][oO].

It is square brackets that denote character classes, whereas curly braces are used for something else entirely (the repetition operator).

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