如何将字符串(通过匹配一组正则表达式)拆分为标记并在JAVA中打印每个标记? [英] How to split the string (by matching a set of regular expression) into tokens and print each token in JAVA ?

查看:83
本文介绍了如何将字符串(通过匹配一组正则表达式)拆分为标记并在JAVA中打印每个标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个字符串s,匹配正则表达式[A-Za-z!,?。_'@] +,将字符串拆分为标记。我们将令牌定义为一个或多个连续的英文字母。然后,打印令牌的数量,然后在新行上打印每个令牌。



输入格式



单个字符串,s。

s由英文字母,空格和以下任何字符组成:!,?。_'@



输出格式



在第一行,打印一个整数n,表示字符串s中的标记数(它们不需要是唯一的)。接下来,按照输入字符串s中显示的顺序打印新行中的每个n个标记。



样本输入 < br $> b $ b

他是一个非常好的男孩,不是吗?



样本输出



Given a string s , matching the regular expression [A-Za-z !,?._'@]+, split the string into tokens. We define a token to be one or more consecutive English alphabetic letters. Then, print the number of tokens, followed by each token on a new line.

Input Format

A single string, s.
s is composed of English alphabetic letters, blank spaces, and any of the following characters: !,?._'@

Output Format

On the first line, print an integer,n, denoting the number of tokens in string s (they do not need to be unique). Next, print each of the n tokens on a new line in the same order as they appear in input string s .

Sample Input

He is a very very good boy, isn't he?

Sample Output

10<br />
He<br />
is<br />
a<br />
very<br />
very<br />
good<br />
boy<br />
isn<br />
t<br />
he





我尝试过:



我的代码:





What I have tried:

My Code:

import java.io.*;
import java.util.*;
import java.util.regex.*; 
public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        scan.close();
       String[] splitString = (s.replaceAll("^[\\W+\\s+]", "").split("[\\s!,?._'@]+"));
            System.out.println(splitString.length);
            for (String string : splitString) {
                System.out.println(string);
              }
}
}







此代码工作正常对于样本输入但没有通过此测试用例。



测试用例:

输入:

<&NBSP;&NBSP;&NBSP;&NBSP;>是&NBSP;&NBSP;&NBSP;&NBSP;前导空格是有效的,problemsetters是evillllll



预期输出:




This code works fine for the Sample Input but do not pass this test case.

Test case:
Input:
<    >YES    leading spaces are valid, problemsetters are evillllll

Expected Output:

8<br />
YES<br />
leading<br />
spaces<br />
are<br />
valid<br />
problemsetters<br />
are<br />
evillllll<br />





有什么变化代码将通过此测试用例?

推荐答案

最后,在重大更改后,我的代码适用于所有测试用例。

如果将来有人遇到同样的问题,请在此处发布。



Finally after major changes, my code worked fine for all test cases.
Posting it here, if anyone in the future comes up with the same problem.

import java.io.*;
import java.util.*;
import java.util.regex.*; 
public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        scan.close();
        if (s.trim().isEmpty()) {
			System.out.println(0);
        }
        else{
        String[] splitString = (s.replaceAll("^\\W+", "").split("[\\s!,?._'@]+"));
        System.out.println(splitString.length);
        for (String string : splitString) {
        	System.out.println(string);
        }
        }
    }
}


这篇关于如何将字符串(通过匹配一组正则表达式)拆分为标记并在JAVA中打印每个标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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