0

Following is the string, Card 41: Slot Type : SFC Card 42: Slot Type : PFC Card 43: Slot Type : GFC Operational State : Empty Card 44: Slot Type : KFC Card 45: Slot Type : SFC

i want to split in a way so that i should have a map of (41,SFC),(42,SFC),(43,GFC),(44,KFC).... currently iam using this regex "\s*Card\s*\d+\s*:", is it possible to split and capture with the same regex, like i mean i want to split with "\s*Card\s*(\d+)\s*:" and capture the (\d+).

3 Answers 3

2

Here's an example of what you want to achieve.

String input = "Card 41: Slot Type : SFC Card 42: Slot Type : " +
                "PFC Card 43: Slot Type : GFC Operational State : Empty " +
                "Card 44: Slot Type : KFC Card 45: Slot Type : SFC";
//                           | starts with "Card"
//                           |   | any white space
//                           |   |   | group 1: any digits
//                           |   |   |     | any characters, reluctantly
//                           |   |   |     |  | group 2: looking for 3 capital letter characters
Pattern p = Pattern.compile("Card\\s+(\\d+).+?([A-Z]{3})");
Matcher m = p.matcher(input);
// key set of map will be ordered lexicographically
// if you want to retain insertion order instead, use LinkedHashMap
// for better performance, just a HashMap
Map<String, String> map = new TreeMap<String, String>();
// iterate over find
while (m.find()) {
    map.put(m.group(1), m.group(2));
}
System.out.println(map);

Output

{41=SFC, 42=PFC, 43=GFC, 44=KFC, 45=SFC}
Sign up to request clarification or add additional context in comments.

Comments

1

To Tokenize, use Capture Groups

This regex will parse your string:

Card (\d+): Slot Type : (\w+)

As you can see in the right pane of the Regex Demo, capture Groups 1 and 2 contain the tuples you want.

Sample Java Code

Here is how to retrieve your tuples:

Pattern regex = Pattern.compile("Card (\\d+): Slot Type : (\\w+)");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    // The Card
    System.out.println(regexMatcher.group(1));
    // The Slot Type
    System.out.println(regexMatcher.group(2));
} 

Of course instead of printing the values, you can assign them to any data structure you like.

Explanation

  • Card matches literal chars
  • (\d+) captures the number to Group 1
  • : Slot Type : matches literal chars
  • (\w+) captures the slot type to Group 2

Comments

0

Card (\d+):.+?: ?(\w+) should do the trick with the global modifier.

  • (\d+) captures the digits after Card
  • (\w+) captures the letters after the second semicolon

Demo on RexEx101

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.