Look for image by class and set as featured image bulk

I’m new here and I have zero knowledge of coding so I hope you can help me.

I migrated an old website to new wordpress one and used a plugins to set first image of posts as featured image. but featured image of some posts for some reason i don’t know are not the first image of the post. so i decided to fix this by coding.

All my first images of the posts has a class name which is "worpresspic". I want that code looks in posts for images that has the class I mentioned above and sets that as post’s featured image despite the post already has featured image or not. i found this regex that seems work perfect at finding the image:

(<img(.*class=['\"](.*)worpresspic(.*)['\"].*)*\/>)+ 

I found this code that look for first image and sets it as featured image as well:

// Get URL of first image in a post    function catch_that_image() {    global $  post, $  posts;    $  first_img = '';    ob_start();    ob_end_clean();    $  output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $  post->post_content,      $  matches);    $  first_img = $  matches [1] [0];     // no image found display default image instead    if(empty($  first_img)){    $  first_img = "/images/default.jpg";    }    return $  first_img;    } 

and also the below code which seems has more options:

// Note that your theme must support post thumbnails for this function to work.  // If you are getting an error try adding add_theme_support('post-thumbnails'); to your functions. php file   function vp_get_thumb_url($  text, $  size){     global $  post;     $  imageurl="";      // Check to see which image is set as "Featured Image"     $  featuredimg = get_post_thumbnail_id($  post->ID);     // Get source for featured image     $  img_src = wp_get_attachment_image_src($  featuredimg, $  size);     // Set $  imageurl to Featured Image     $  imageurl=$  img_src[0];      // If there is no "Featured Image" set, move on and get the first image attached to the post     if (!$  imageurl) {         // Extract the thumbnail from the first attached imaged         $  allimages =&get_children('post_type=attachment&post_mime_type=image&post_parent=' . $  post->ID );          foreach ($  allimages as $  img){             $  img_src = wp_get_attachment_image_src($  img->ID, $  size);             break;         }         // Set $  imageurl to first attached image         $  imageurl=$  img_src[0];     }      // If there is no image attached to the post, look for anything that looks like an image and get that     if (!$  imageurl) {         preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i' ,  $  text, $  matches);         $  imageurl=$  matches[1];     }      // If there's no image attached or inserted in the post, look for a YouTube video     if (!$  imageurl){         // look for traditional youtube.com url from address bar         preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/watch(\?v\=|\/v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $  text, $  matches2);         $  youtubeurl = $  matches2[0];         $  videokey = $  matches2[3];     if (!$  youtubeurl) {         // look for youtu.be 'embed' url         preg_match("/([a-zA-Z0-9\-\_]+\.|)youtu\.be\/([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $  text, $  matches2);         $  youtubeurl = $  matches2[0];         $  videokey = $  matches2[2];     }     if ($  youtubeurl)         // Get the thumbnail YouTube automatically generates         // '0' is the biggest version, use 1 2 or 3 for smaller versions         $  imageurl = "http://i.ytimg.com/vi/{$  videokey}/0.jpg";     }      // Spit out the image path     return $  imageurl; } 

But as I said I have no coding knowledge and don’t know how to edit these codes to match my needs. For example this codes check if the post has featured image or not and if the post has featured image nothing happens, but i don’t want this.

Also I want to run it in wordpress and without need to open each post and save, all featured images of the posts being updated. where to put the code? what php code I need to add and where can i put the final code? (as i know it must be functions.php am i right?)

Thank you for your help.