• Resolved RA_NPL

    (@ra_npl)


    Hello,

    I am running https://www.ra-plutte.de, which is a lawyer website with loads of legal posts. Recently I noticed that searching for court rulings / judgements based on their file number does not return proper results.

    Example: If you search for 6 U 14/19 on https://www.ra-plutte.de, no result is returned. However, it should return the blog post https://www.ra-plutte.de/olg-frankfurt-irrefuehrung-werbung-gekaufte-bewertungen/ which contains the string in the first sentence.

    I found a post in your blog covering bible verses but I am not sure if this works for my case because the suggested solution (search in quotation marks) did not work in my test meaning that a search for "6 U 14/19" in my blog also returns 0 results.

    Can you help me out on this?

    Best, Nick

    The page I need help with: [log in to see the link]

Viewing 4 replies - 16 through 19 (of 19 total)
  • Plugin Author Mikko Saari

    (@msaari)

    You must have some delimiters for the regex, and I’m using # to avoid the need to escape the /. You can just as well have / as the delimiters and escape the /.

    As for the \1\2\3\4, that’s the replacement for the capturing groups. Because the different patterns have different number of parts, it may be clearer to do multiple separate replacements:

    $patterns = array(
      '#(\d{1,3}\-)?(\d{1,3})\s(\w)\s(\d{1,5})/(\d\d)#',
      '#(\w)-(\d{1,4})/(\d\d)#',
      '#(\w{1,2})\s(\w{2,3})\s(\d{1,4})/(\d\d)#',
      '#(\w\w\w)\s(\d+)/(\d+)#',
    );
    $replacements = array(
      '\1\2\3\4\5',
      '\1\2\3',
      '\1\2\3\4',
      '\1\2\3',
    );
    $a = preg_replace( $patterns, $replacements, $a );

    I also corrected some of your patterns: they had white space and separators inside the capturing groups when the point is to have only the letters and the digits in the capturing groups so that everything else gets filtered out.

    But good work on the regexes here!

    Thread Starter RA_NPL

    (@ra_npl)

    Thanks and awesome, I think I get what you are saying. To double-check: In the second row, you wrote

    '#(\d{1,3}\-)?(\d{1,3})\s(\w)\s(\d{1,5})/(\d\d)#',

    Do I not need to de-escape the \-? If so, would this be the final code?

    // Custom Fix for Relevanssi plugin to detect court rulings in searches
    add_filter( 'relevanssi_remove_punctuation', 'rlv_convert_filenumbers', 9 );
    add_filter( 'relevanssi_post_content', 'rlv_convert_filenumbers' );
    function rlv_convert_filenumbers( $a ) {
    	$patterns = array(
      	'#(\d{1,3}-)?(\d{1,3})\s(\w)\s(\d{1,5})/(\d\d)#',
      	'#(\w)-(\d{1,4})/(\d\d)#',
      	'#(\w{1,2})\s(\w{2,3})\s(\d{1,4})/(\d\d)#',
      	'#(\w\w\w)\s(\d+)/(\d+)#',
    	);
    	$replacements = array(
      	'\1\2\3\4\5',
      	'\1\2\3',
      	'\1\2\3\4',
      	'\1\2\3',
    	);
    $a = preg_replace( $patterns, $replacements, $a );
      return $a;
    }
    Plugin Author Mikko Saari

    (@msaari)

    Ah, yes, you do need to escape the hyphen. It’s also a good idea to save some individual posts after adding the new filter to see that the file numbers are converted correctly, before reindexing everything.

    Thread Starter RA_NPL

    (@ra_npl)

    Great stuff, thanks for your help. Works like a charm!

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘Search for exact multi-part strings’ is closed to new replies.