Java – search for tags and save the text between tags as variables

I'm new to Java, but how to search for tags in files, and then all the contents between tags (such as a string of text) will be assigned to variables

For example, I have < title > the title < / Title >, but then I want to save the string "the title" to a variable named title1 or something else

What should I do? thank you.

Solution

If you use regular expressions, you only need to use the capture group:

Pattern p = Pattern.compile("<title>([^<]*)</title>",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(theText);
if (m.find()) {
    String thisIsTheTextYouWant = m.group(1);
    ....
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>