2

I am trying to extract specific characters from string. I have tried using Split and replace to get data. But any other alternative is there to extract? Following is input string

  Input1-    
    q={!tag=tagForType}(Type:(ABC))

  Input2-  
    q={!tag=tagForType}(Type:(ABC OR XYZ))

Output required in list format.

  Output1-  List1{ABC}
  Output2- List1{ABC ,XYZ)

Following is code I have tried to extract such data

 if (s.contains("Type")) {                               
    List = s.split("Type:\\(");
    String s1 = List[1].replaceAll("\\W", "");
    List1 = s1.split("OR");                                
 }

Any other alternative?

5
  • 3
    This doesn't look like JavaScript. You cannot declare a variable with String type. Commented Jan 2, 2014 at 8:58
  • 1
    Shouldn't it be tagged java? Commented Jan 2, 2014 at 8:59
  • Sorry it was java.. Thanks for informing Commented Jan 2, 2014 at 9:00
  • 2
    Why do you want the alternative method ? Do you face any performance issue Commented Jan 2, 2014 at 9:05
  • Instead of split and relaceAll method.. is there any better alternative to extract data? Commented Jan 2, 2014 at 9:10

2 Answers 2

2

See this solution with regexes.

    String input = "q={!tag=tagForType}(Type:(ABC OR XyZ OR ORT))(Type:(ABC))";
    Pattern findType = Pattern.compile("Type:(\\([ \\w]+\\))");
    Pattern extractLists = Pattern.compile("(\\(| OR )([\\w]+)");
    Matcher typeMatcher = findType.matcher(input);
    while (typeMatcher.find()) {
        System.out.println(typeMatcher.group(1));

        Matcher listMatcher = extractLists.matcher(typeMatcher.group(1));
        while (listMatcher.find()) {
            System.out.println(listMatcher.group(2));
        }
    }

This prints the following:

(ABC OR XYZ OR ORT)
ABC
XyZ
ORT
(ABC)
ABC

Of course you might need to do something else with the groups, I currently am just printing them out.

Note that here I demonstrate how this solution works with multiple Type: in the same string, which I think your solution will not handle.

Also currently I am assuming the amount of intervals in between the parts is fixed, but this can also be worked on if using regexes.

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

Comments

0

Try This :

 String EXAMPLE_TEST = "q={!tag=tagForType}(Type:(ABC))";

 pattern = "^.*?Type:.*?([\\w\\s]+).*";

 String updated = EXAMPLE_TEST.replaceAll(pattern, "$1"  ); 

 System.out.println(updated);

For splitting with OR

String[] split = updated.split(" OR ");
for(int i=0;i<split.length;i++) 
 System.out.println(split[i]);

1 Comment

This will not handle the ` OR ` splitting I think.

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.