拆分String的数字列表

时间:2016-05-05 17:21:17

标签: java regex

我有String的列表,并且没有正则表达式的技能。所以我需要你的帮助。

第一个字符串:

[begin]
65. This tutorial walks you through a series of exercises to get familiar with Kotlin. 
66. Kotlin Koans are a series of exercises to get you familiar with the Kotlin Syntax.
67. Each exercise is created as a failing unit test and your job is to make it pass. You can play with Koans online or offline by cloning the project on GitHub:
...
[end]

第二个字符串

[begin]
63. Download the Koans by cloning the project from GitHub
64. Open up the project in IntelliJ IDEA or your favorite editor. Note: If IntelliJ IDEA prompts you to update the Kotlin library, just click yes.
[end]

和其他字符串......

目标:

我需要将每个数字行提取为String

String#1 : 63. Download the Koans by cloning the project from GitHub
String#2 : 64. Open up the project in IntelliJ IDEA or your favorite editor. Note: If IntelliJ IDEA prompts you to update the Kotlin library, just click yes.
String#3 : 65. This tutorial walks you through a series of exercises to get familiar with Kotlin. 
String#4 : 66. Kotlin Koans are a series of exercises to get you familiar with the Kotlin Syntax.
String#5 : 67. Each exercise is created as a failing unit test and your job is to make it pass. You can play with Koans online or offline by cloning the project on GitHub:

P.S。非常感谢任何技能正则表达式的在线培训工具。

3 个答案:

答案 0 :(得分:2)

我会这样:

  1. 定义一个类来解析行和文本..(你需要对它进行排序List

    class TextLine implements Comparable<TextLine> {
    
        private int line;
        private String lineText;
    
        public TextLine(int line, String lineText) {
            this.line = line;
            this.lineText = lineText;
        }
    
        @Override
        public String toString() {
            return "String:" + line + lineText;
        }
    
        @Override
        public int compareTo(TextLine o) {
            return Integer.compare(this.line, o.line);
        }
    }
    
  2. 阅读并解析您在List中找到的所有行(您是对的,正则表达式是获取它的一种方式)

  3. 使用Collections.sort ...

  4. 对列表进行排序
  5. 打印

  6. 示例:

    public class TestApp {
    
        private List<TextLine> output = new ArrayList<TextLine>();
    
        public static void main(String[] args) {
            TestApp tt = new TestApp();
            String str = "65. This tutorial walks you through a series of exercises to get familiar with Kotlin. ";
            tt.populateLsit(str);
            str = "67. Each exercise is created as a failing unit test and your job is to make it pass. You can play with Koans online or offline by cloning the project on GitHub:";
            tt.populateLsit(str);
            str = "66. Kotlin Koans are a series of exercises to get you familiar with the Kotlin Syntax. ";
            tt.populateLsit(str);
            str = "63. Download the Koans by cloning the project from GitHub";
            tt.populateLsit(str);
            str = "64. Open up the project in IntelliJ IDEA or your favorite editor. Note: If IntelliJ IDEA prompts you to update the Kotlin library, just click yes.";
            tt.populateLsit(str);
            tt.sortPrint();
        }
    
        private void sortPrint() {
            Collections.sort(output);
            System.out.println(output);
    
        }
    
        private void populateLsit(String str) {
            Matcher match = Pattern.compile("^(\\d+)|(.*)").matcher(str);
            // String r = match.group();
            match.find();
            int i = Integer.parseInt(match.group());
            match.find();
            String s = match.group();
            output.add(new TextLine(i, s));
        }
    }
    

    Here is a good starting point for regex

答案 1 :(得分:0)

这个适用于javascript /^(\d+)/gm

https://regex101.com/r/fZ9lV1/1

答案 2 :(得分:0)

我希望您的正则表达式看起来像^(\d+.*)

^ - start of line
\d+ - any non zero amount of numbers
.*  - any symbols
( ) - grouping

你可以在:rubular.com

上玩它