Home > front end >  Extracting a substring from a given string pattern
Extracting a substring from a given string pattern

Time:09-22

Here are the Strings:

Example 1 - Movie=HULK/Incredible HULK;old_actor=Edward Norton;new_actor=Mark Ruffalo

Example 2 - Movie=HULK/Incredible HULK;old_movie_release_date=12 December 2008;new_movie_release_date=20 June 2012

How can I extract values like old_actor, new actor from example 1 and new_movie_release_date and old_movie_release_date from example 2.

I'm new to regex trying to see how can this be done. Thanks in advance.

CodePudding user response:

You can do using java regex as follows

String str1 = "Movie=HULK/Incredible HULK;old_actor=Edward Norton;new_actor=Mark Ruffalo";
String str2 = "Movie=HULK/Incredible HULK;old_movie_release_date=12 December 2008;new_movie_release_date=20 June 2012";

String pattern1="Movie=(.*?);old_actor=(.*?);new_actor=(.*?)$";
String pattern2="Movie=(.*?);old_movie_release_date=(.*?);new_movie_release_date=(.*?)$";

Matcher m1 =  Pattern.compile(pattern1).matcher(str1);
if (m1.find()) {    
    System.out.println("old_actor: "   m1.group(2));
    System.out.println("new_actor: "   m1.group(3));
}  

Matcher m2 = Pattern.compile(pattern2).matcher(str2);
if (m2.find()) {    
    System.out.println("old_movie_release_date: "   m2.group(2));
    System.out.println("new_movie_release_date: "   m2.group(3));
}

CodePudding user response:

You could use String.split(String regex). First, you use String.split(";"), which will give you an array String[] values with contents looking like Movie=moviename, then you use String.split("=") on each string in the first array

for(String str : values) {
String[] keyValue = str.split("=");
}

to create subarrays of length 2 with key at position 0 and value at position 1.

CodePudding user response:

Just an enhancement to @DerLebkuchenmann's solution

  public static void main(String[] args) {
    String str1 = "Movie=HULK/Incredible HULK;old_actor=Edward Norton;new_actor=Mark Ruffalo";
    String str2 = "Movie=HULK/Incredible HULK;old_movie_release_date=12 December 2008;new_movie_release_date=20 June 2012";

    Map<String, String> props1 = getProps(str1);
    Map<String, String> props2 = getProps(str2);

    System.out.println(String.format("Old Actor: %s", props1.get("old_actor")));
    System.out.println(String.format("Old Movie Release Date: %s", props2.get("old_movie_release_date")));
    System.out.println(String.format("New Movie Release Date: %s", props2.get("new_movie_release_date")));
  }

  private static Map<String, String> getProps(String str1) {
    return Arrays.stream(str1.split(";"))
        .map(pair -> pair.split("="))
        .collect(Collectors.toMap(crumbs -> crumbs[0], crumbs -> crumbs[1]));
  }

CodePudding user response:

Another approach using StringTokenizer and assembling a HashMap for result:

public class Main
{
    public static void main(String[] args) {
        HashMap<String,String> m = new HashMap<String,String>();
        StringTokenizer st = new StringTokenizer("Movie=HULK/Incredible HULK;old_actor=Edward Norton;new_actor=Mark Ruffalo",";=");
        while(st.hasMoreTokens()) {
            String s = st.nextToken();
            if (st.hasMoreTokens()) { // ensure well-formed
                m.put(s,st.nextToken());
            }
        }
        System.out.println(m);
    }
}

Prints:

{Movie=HULK/Incredible HULK, old_actor=Edward Norton, new_actor=Mark Ruffalo}
  • Related