-3

I'm trying to split this string :

Testing package [Store Management](https://www.Testing.com/content/zone_store_mgmt_policy\"Store Management Policy") Test package2

using regex, so it splits into 3 parts:

  1. Testing package
  2. [Store Management] (https://www.Testing.com/content/zone_store_spl_policy\"Store Management Policy")
  3. Test package2

The rule is split the string when the pattern [any characters](any characters) is found.

This is what I tried :

String values ="Testing package [Store Management](https://www.Testing.com/content/zone_store_spl_policy\"Store Management Policy\") Test package2";
pattern = Pattern.compile("(?:\\[.*\\]\\(.*\\))");
valueArray = pattern.split(values);
for (String s: valueArray) 
           System.out.println(s); 

But the output is:

Testing package Test package2

I need an array containing 3 strings as above.

Teena
  • 1
  • 1
  • Please familiarize yourself with the editor in StackOverflow. Take some time and format your question properly. It is unreadable in the current state. – Zabuzard Feb 24 '21 at 21:04
  • It's not clear what exactly your string is because you haven't set it apart in any way. As far as I can tell `Test package2 using regex. so it looks like this array` is part of the string, but I'm sure "so it looks like this array" isn't really part of it. – Stephen P Feb 24 '21 at 21:05
  • The problem is that you have a `?` at the end making `""` valid for the regex. This is why it detects every character alone. – dan1st Feb 24 '21 at 21:13
  • When I removed the ? at the end I got the output as Testing package Test package2 but the value [Store Management] (https://www.Testing.com/content/zone_store_spl_policy\"Store Management Policy\") is missing in the array. – Teena Feb 24 '21 at 21:16
  • Use [this code](https://ideone.com/ghjBu8) (see [regex demo](https://regex101.com/r/cDvOvq/1)). – Wiktor Stribiżew Feb 24 '21 at 21:23
  • Tried it but got 4 elements as below[Testing package, [Store Management], (https://w...content-available-to-author-only...g.com/content/zone_store_mgmt_policy"Store Management Policy"), Test package2] I need 3 elements whatever comes in []() as one element [Store Management] (https://w...content-available-to-author-only...g.com/content/zone_store_mgmt_policy"Store Management Policy") – Teena Feb 24 '21 at 21:37
  • Great, then it is even simpler: [here is your solution](https://stackoverflow.com/a/44084475/3832970). – Wiktor Stribiżew Feb 24 '21 at 21:40

1 Answers1

1

It will be far simpler to use group matching then simple split as you have different delimiters for each portion

Try the following (using very naïve pattern matching)

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {
   public static void main( String args[] ) {
      String line = "Testing package [Store Management](https://www.Testing.com/content/zone_store_mgmt_policy\"Store Management Policy\") Test package2";
      String pattern = "(.*)\\s*(\\[.*\\])(\\(.*\\))\\s*(.*)";

      // Create a Pattern object
      Pattern r = Pattern.compile(pattern);

      // Now create matcher object.
      Matcher m = r.matcher(line);
      
      if (m.find( )) {
         System.out.println(m.group(1));
         System.out.println(m.group(2));
         System.out.println(m.group(3));
         System.out.println(m.group(4));
      } else {
         System.out.println("NO MATCH");
      }
   }
}

Some reference:

On capturing groups in regex

Java Matcher

Boaz
  • 4,549
  • 2
  • 27
  • 40