Raje
Raje

Reputation: 3333

Extract specific string in java

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?

Upvotes: 2

Views: 166

Answers (2)

Boris Strandjev
Boris Strandjev

Reputation: 46943

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.

Upvotes: 2

Nishant Lakhara
Nishant Lakhara

Reputation: 2445

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]);

Upvotes: 0

Related Questions