分割字符串的数字列表
我有一个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:
PS将非常感谢任何在线技能正则表达式的在线培训工具。
这个适用于javascript /^(\d+)/gm
我会走这条路:
-
定义一个类来解析行和文本..(你将需要那个排序最终
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); } }
-
阅读和解析
List
找到的所有行(你是对的,正则表达式是获得它的一种方法) -
使用
Collections.sort
对列表进行排序… -
打印
例:
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)); } }
这是一个正则表达式的好起点 。
我希望你的正则表达式看起来像下面的^(\d+.*)
其中
^ - start of line \d+ - any non zero amount of numbers .* - any symbols ( ) - grouping
你可以在rubular.com上玩