String manipulation

I have strings ex: <null>, <test|test1|test3|test4|null>. Between the angle brackets if I see pipe delimited values, I need to get each value. If I see null without pipes, I need to process database updaate. Could anyone provide a sample code????
newuser11Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
try this:

    public static void main(String[] args) {
        String theInput = "<null>, <test|test1|test3|test4|null>";
        String[] tokens = theInput.split("[,<>|]");
        for(int i=0; i < tokens.length; i++)
        {
            System.out.println(tokens[i]);
        }
    }
0
petmagdyCommented:
or better:

public class SampleParse {
   
    public static void doSingleDdOperation(String row)
    {
        String[] values = row.split("[,><|]");
                for(int i=0; i < values.length; i++)
                {
                    System.out.println(values[i]);

                }
       
    }
   
    public static void main(String[] args) {
        String theInput = "<null>, <test|test1|test3|test4|null>";
        String[] rows = theInput.split("null>");
        for(int i=0; i < rows.length; i++)
        {
            System.out.println(rows[i]);
            doSingleDdOperation(rows[i]);
        }
    }
}
0
newuser11Author Commented:
Thank you so much for your help. Can you also help me with the following regular expression.

String theInput = "<test,tester|test1|test3,tester|test4|null, one,two|three|four>";

If I have a "," in between the "test,tester", I want to return this as one name. For the split regular expression,  how to say return only

test,tester|test1|test3,tester|test4|null

one,two|three|four

as two tokens.

Thanks.


0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

petmagdyCommented:
then try this:

      public static void main(String[] args) {
            String theInput = "<test,tester|test1|test3,tester|test4|null, one,two|three|four>";
            String[] rows = theInput.split("(<)|(>)|(, )");
            for(int i=0; i < rows.length; i++)
            {
                  System.out.println(rows[i]);
//                  doSingleDdOperation(rows[i]);
            }
      }
0
newuser11Author Commented:
Thanks one more time.

The actual string I am trying with is

<[10307|sdme|20011002        |xxxx, xxxxx||||||AF|RURAL||||20010924|20880808, 10308|sdme|20011002        |YYY YYYY, TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808,null]>
From this string I want

10307|sdme|20011002        |xxxx, xxxxx||||||AF|RURAL||||20010924|20880808
10308|sdme|20011002        |YYY YYYY, TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808

as two tokens.  split("(<)|(>)|(, )") is giving four tokens.

10307|sdme|20011002        |xxxx
xxxxx||||||AF|RURAL||||20010924|20880808
10308|sdme|20011002        |YYY YYYY
TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808

Can you help again with this.

Thanks.
 






0
petmagdyCommented:
then ur regex will look like:

"(<[)|(]>)|(, )|(,null)"
0
newuser11Author Commented:
No, it's still giving me four tokens.  In this regex, how to write apply comma rule only after 16 pipe.

Thanks.

0
RMaruszewskiCommented:
You shouldn't do it.
You should have more general method to parse such strings.

If you want to parse this line:
<[10307|sdme|20011002        |xxxx, xxxxx||||||AF|RURAL||||20010924|20880808, 10308|sdme|20011002        |YYY YYYY, TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808,null]>

Use this algorithm:

1. Cut off "<[" and "]>" from the beginning and the end of the string:
  input = input.substring(2, input.length() - 2);

2. Split the string on '|' character:
  String[] fields = input.split("\|");

3. Get fields number 1 and 16:
  String field1 = fields[0]; // should be "10307"
  String field16 = fields[15]; // should be "20880808, 10308"

4. Split field16 on ',' character:
  String[] parts = field16.split(",");

5. Get both parts from field16, and trim them if necessary:
  String part1 = parts[0].trim(); // should be "20880808"
  String part2 = parts[1].trim(); // should be "10308"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
sorry the code is not very optimized but I have no more time to think it over but works however:

import java.util.Vector;

public class RegexTest {
       
    public static String[] separateAfter16Pipe(String singleRow)
    {
        String[] elements  = singleRow.split("\\Q|\\E");
        String[] results = null;
        System.out.println(elements.length);
        if(elements.length < 16)
        {
            results = new String[1];
            results[0]=singleRow;
            return results;
        }
        Vector resultVector = new Vector();
        int j=0;
        String concat = "";
        for(int i=0; i < elements.length; i++)
        {
            if(j==15)
            {
                String[] splited = elements[i].split(", ");
                concat += splited[0];
                resultVector.add(concat);
                concat = "";
                if(splited.length >= 2)
                {
                    concat += splited[1];
                    if(i < (elements.length-1) )
                    {
                        concat += "|";
                    }
                }
                j=0;
            }
            else if(i == (elements.length-1))
            {
                concat += elements[i];
                resultVector.add(concat);            
            }
            else
            {
                concat += elements[i];
                if(i < (elements.length-1) )
                {
                    concat += "|";
                }
                j++;
            }
        }
        results = (String[]) resultVector.toArray(new String[0]);
        return results;
    }
   
    public static void printArrayElements(String[] arr)
    {
        for(int i=0; i < arr.length; i++)
        {
            System.out.println(arr[i]);
        }        
    }
   
    public static void main(String[] args) {
        String theInput = "<[10307|sdme|20011002        |xxxx, xxxxx||||||AF|RURAL||||20010924|20880808, 10308|sdme|20011002        |YYY YYYY, TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808,null]>";
        String[] rows = theInput.split("(\\Q<[\\E)|(\\Q]>\\E)|(,null)");
        for(int i=0; i < rows.length; i++)
        {
            System.out.println(rows[i]);
            String[] result = separateAfter16Pipe( rows[i]);
            printArrayElements(result);
        }
    }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.