English 中文(简体)
引伸缩模式
原标题:Pattern for pulling strings out a string

I m not new to Java, but have not dealt with Regex and Patterns before. What I m looking to do is take a string like

"Class: " + data1 + "
From: " + data2 + " To: " + data3 + "
Occures: " + data4 + " In: " + data5 + " " + data6;

and pull out only data_1 to data_n.
I appreciate any help.

最佳回答

Use this regex:

Pattern pattern = Pattern.compile("Class: (.+?)
From: (.+?) To: (.+?)
Occures: (.+?) In: (.+?) (.+?)");
Matcher matcher = pattern.matcher(yourInputString);
if (matcher.find())
{
     String data1 = matcher.group(1);
     String data2 = matcher.group(2);
     String data3 = matcher.group(3);
     String data4 = matcher.group(4);
     String data5 = matcher.group(5);
     String data6 = matcher.group(6);
} else
{
    // String didn t match the specified format
}

解释:

.+? will match any character for undefined times, but non-greedy.
(), using brackets will create a group. A group is given an index starting by 1 (since group 0 is the entire match)

因此,<代码>(+?)将产生任何性质的群体。

而匹配者所做的是,正在寻找整个模式,在投入体中占据一定位置。 但是,既然你指明了这一形式,我们就确切知道,你的整个努力将如何看待。 唯一需要做的是复制格式并替换你希望以“<>tthing<>>>(+?)提取的数据。

之后,配对人将设法找到这一模式(通过<条码>matcher.find(),请他们看一至六组的内容。

问题回答

如何使用“:”的分离,然后从分裂的强权中分离出来[2i+1]? (i from 0)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签