简体   繁体   中英

Substring selection between two Strings

I'm doing some random Java work, and my app, saves a file with data like:

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. 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* zero or more whitespace
  • (\\\\w+) one or more of characters of type 0-9 , az , AZ or _ . 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:

Word1
Desc1
1
Word2
Desc2
2
Word3
Desc3
3
Word4
Desc4
4

This regex applies to the above data:

(\\b\\w+\\b)(?!:)

What does this regex mean:

  1. Begin a capture group (
    1. Match a word boundary \\b
    2. Match alphanumeric characters \\w between 1 and unlimited times +
    3. Match a word boundary \\b
  2. Close the capture group )
  3. Assert that the following CANNOT be matched starting from this position (?! (negative lookahead)
    1. The character : literally
  4. Close the negative lookahead )

One Liner:

String str = "Word: Word1 Description: Desc1 Type: 1";

// Output: ["Word1", "Desc1", "1"]
str.replaceFirst(" ?\\w*: ", "").split(" ?\\w*: ");

You can use 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM