[英]Substring selection between two Strings
I'm doing some random Java work, and my app, saves a file with data like: 我正在做一些随机的Java工作,而我的应用程序用以下数据保存文件:
Word: Word1 Description: Desc1 Type: 1
Word: Word2 Description: Desc2 Type: 2
Word: Word3 Description: Desc3 Type: 3
Word: Word4 Description: Desc4 Type: 4
It saves it succesfully, when trying to retrieve the data, I'm unable to find out what regex filter I should apply. 它成功地保存了它,在尝试检索数据时,我无法找出应该应用的正则表达式过滤器。 For example, from line:
例如,从行:
Word: Word1 Description: Desc1 Type: 1
I'd like to extract: 我想提取:
Word1
Desc1
1
Each one in different Strings. 每一个都在不同的字符串中。
I just don't end to understand Patterns syntax, and it's giving me a headhache already. 我只是不了解模式语法,它已经给了我一个头脑。 Thanks for your time :)
谢谢你的时间 :)
----------------- EDIT ---------------- -----------------编辑----------------
Thanks you all! 谢谢大家! I finally used Kon's answer.
我终于使用了Kon的答案。 The resulting code was much simplier that I thought.
结果代码比我想象的要简单得多。 I'm leaving the code for anyone who may have a similar problem.
我将代码留给可能遇到类似问题的人。
package resources;
import resources.manager.Word;
public class CommonFunctions {
public static Word parseString(String str){
String[] stringA = str.split(" ");
Word result = new Word(stringA[1],stringA[3],Integer.parseInt(stringA[5]));
return result;
}
public static String parseWord(Word wrd){
//TODO
return null;
}
}
It seems that you are looking for words or numbers that are placed after :
. 您似乎正在寻找放置的文字或数字
:
。 You can use this regex :\\\\s(\\\\w+)
which means represents 你可以使用这个正则表达式
:\\\\s(\\\\w+)
表示代表
:
\\\\s*
zero or more whitespace \\\\s*
零个或多个空格 (\\\\w+)
one or more of characters of type 0-9
, az
, AZ
or _
. (\\\\w+)
0-9
, az
, AZ
或_
一个或多个字符。 Also by surrounding it with parenthesis regex will place this part of match in group 1 Demo: 演示:
String[] data = { "Word: Word1 Description: Desc1 Type: 1 ",
"Word: Word2 Description: Desc2 Type: 2 ",
"Word: Word3 Description: Desc3 Type: 3 ",
"Word: Word4 Description: Desc4 Type: 4 " };
Pattern p = Pattern.compile(":\\s*(\\w+)");
for (String s:data){
Matcher m = p.matcher(s);
while (m.find())
System.out.println(m.group(1));
}
Outpt: OUTPT:
Word1
Desc1
1
Word2
Desc2
2
Word3
Desc3
3
Word4
Desc4
4
This regex applies to the above data: 此正则表达式适用于以上数据:
(\\b\\w+\\b)(?!:)
(
(
\\b
\\b
\\w
between 1 and unlimited times +
\\w
在1和无限次之间+
\\b
\\b
)
)
(?!
(negative lookahead) (?!
(负向前瞻)
:
literally :
字面意思 )
)
String str = "Word: Word1 Description: Desc1 Type: 1";
// Output: ["Word1", "Desc1", "1"]
str.replaceFirst(" ?\\w*: ", "").split(" ?\\w*: ");
You can use StringTokenizer: 你可以使用StringTokenizer:
String str = "Word: Word1 Description: Desc1 Type: 1";
StringTokenizer st = new StringTokenizer(str," ");
st.nextToken();
String word = st.nextToken();
St.nextToken();
String description = st.nextToken();
st.nextToken();
String type = st.nextToken();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.