0

I am trying to create a Map from a multi-line String with various patterns. For example, considering the following String:

:20:94001142322029214336
    1234
    6789
:86F:/PG/1L123
:25|11298666
:28G::20/1345
      xyz

My patterns are like:

starting : OR :: OR | OR || and ending with : OR :: OR | OR || I am trying to create a map as follows:

Key   = 20
Value = 94001142322029214336
        1234
        6789  

Key =   86F
Value = /PG/1L

Key = 25 
Value = 11298666

Key  = 28G   

Value = 20/1 xyz

The value can be multi line so i need all the lines from value to be part of my map value for that key.

Could you please help me to create the Java regex or any other solution that can help me to create this map?

5
  • You're trying to create a Map with the same blank key for different values. That's not going to work.... Commented Mar 23, 2014 at 3:22
  • Key is 20 but value is multi line value...its not blank key Commented Mar 23, 2014 at 3:23
  • the examples are very confusing.. pls try to clarifu Commented Mar 23, 2014 at 3:24
  • it is not confusing. He has multiline text. Keys are enclosed within combination of : and | and values may or may not be multiline text. Commented Mar 23, 2014 at 3:26
  • @Nishant it was before this edit Commented Mar 23, 2014 at 3:28

2 Answers 2

1

Split the string with

String[] lineElements = line.split("(?:\\:|\\|){1,2}");

Either there's only one element, meaning there's no key (in this case you'll need to use the most-recent one, which you'll need to keep track of), or there are three elements:

  1. Always empty
  2. The key
  3. The value

To allow for the reverse of this process (to recreate the input file, based on the already-parsed data), you could store each line's data into its own object, such as

public class WhateverDataObject  {
   public final String key;
   public final String value;
   public WhateverDataObject(String key, String value)  {
      this.key = key;
      this.value = value;
   }
   public String toString()  {
      ":" + key + ":" + value;
   }
}

Then output each object's toString().

If you also need the delimiters to be exactly as they were, that's a lot more work, as you'll need to pre-analyze each line, and add those elements to the DataObject, including its toString().

Sign up to request clarification or add additional context in comments.

6 Comments

String can start with | or || or :: as well, so will this regex give me correct values? ::20:1234 ||20:|1234 etc.
Yes, I mention that in my answer: The consequence is that the first element in the three-element array (when there is a key) will always be empty.
Thanks a ton for the quick help...I have another requirements to replace the value in the similar string for specific tag, the tag may repeat many times, so want to replace specific occurrence so can't use replace or replaceAll and after replacing the tag value i have to write the entire string again with delimiters, for example :20:1234\n:28G::xyz\n|20:3456\n:29C:pqr\n:20|9876 so 3456 needs to be replaced from tag 20 for second occurrence with 5678 so the final string should look like :20:1234\n:28G::xyz\n|20:5678\n:29C:pqr\n:20|9876 any tips on how to do it..?
I'll answer you here, but this really should have either been in your original question, or posted as a new question. Give me a minute, and I'll update my answer.
Sorry for not adding it into my original question, i was planning to create new question but thought will post it here to get quick answer :)
|
0

you can try and use this ..

Replace all possible Key identifiers with just one.. then split using them..

import java.util.Arrays;
import java.util.HashSet;

public class HelloWorld{

     public static void main(String []args)
     {
        String input = ":20:94001142322029214336\n1234\n6789\n:86F|/PG/1L123\n:25||11298666\\:28G::20/1345\nxyz";
         System.out.println(input+"\n\n");

        input=input.replace("::",":");
        input=input.replace("||",":");
        input=input.replace("|",":");
         System.out.println(input+"\n\n");

        String words[] = input.split(":");
        for(int index = 1;index<words.length;index++)
        {
             System.out.println("Key = "+words[index]+"\nValue = "+words[++index]);
        }
     }

1 Comment

Just style, but I'd choose a single regex replacement: Find what="(:|\|){1,2}", Replace with=":". (The character class [:|] would be inappropriate as it would replace invalid separators, such as "|:".)

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.