stri_replace_all_fixed等效项:根据列表

时间:2017-12-13 22:41:15

标签: c# python regex string perl

R有一个名为stringi的强大字符串处理包。特别是,我希望实现与stri_replace_all_fixed函数相同的功能:基于列表/字典替换匹配,例如Perl / PHP / Python或C#。

#install.packages("stringi")
library(stringi)
stri_replace_all_fixed("The quick brown fox jumped over the lazy dog.",
    c("quick", "brown", "fox"), c("slow",  "black", "bear"), vectorize_all=FALSE)

输出:

  “慢慢的黑熊跳过懒狗。”

3 个答案:

答案 0 :(得分:2)

使用哈希来映射要替换的替换词。

use warnings;
use strict;
use feature 'say';

my $string = 'The quick brown fox jumped over the lazy dog.';

my @words     = qw(quick brown fox);
my @change_to = qw(slow black bear);

my %replacement;
@replacement{@words} = @change_to;  # populate hash: quick => 'slow', ...

my $pattern = join '|', map { quotemeta } sort { length $a < length $b } @words;

$string =~ s/($pattern)/$replacement{$1}/g;

say $string;

请注意,我们需要sort模式的length组件,以便更长的单词出现在较短的单词之前;否则,/no|none/ 'no'可以替换'none'中的sub_name($string, \@words, \@change_to);

将其包裹在具有所需界面的子程序中,例如

$(document).ready(function(){
    $('button').click(function(){
        var data = $('#txt').val();
        if(data == '') return;

        JSONToCSVConvertor(data, "Agenda", true);
    });
});

function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
    //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
    var arrData = typeof JSONData != 'object'
      ? JSON.parse(JSONData)
      : JSONData;

    var CSV = '';
    //Set Report title in first row or line
    CSV += ReportTitle + '\r\n\n';



    //This condition will generate the Label/Header
    if (ShowLabel) {
      var row = "";

      //This loop will extract the label from 1st index of on array
      for (var index = 0; i<arrData.length-1; index++) {
        if (index  == 4 || index == 5 || index == 6 || index == 7 || index == 8) {
          continue;
        }
        else{
          //Now convert each value to string and comma-seprated
          row += index + ';';

        row = row.slice(0, -1);
        //append Label row with line break
        CSV += row + '\r\n';
        }
    }
    //1st loop is to extract each row
    for (var i = 0; i < arrData.length-1; i++) {
      if (i  == 4 || i == 5 || i == 6 || i == 7 || i == 8) {
        continue;
      }
      else{
        var row = "";
      }
      //2nd loop will extract each column and convert it in string comma-seprated
      for (var j = 0; i<arrData.length-1; j++) {
          if (j  == 4 || j == 5 || j == 6 || j == 7 || j == 8) {
            continue;
          }
          else{
            row += '"' + arrData[i][j] + '";';
          }
        }
      }

      row.slice(0, row.length - 1);

      //add a line break after each row
      CSV += row + '\r\n';
    }



    if (CSV == '') {
      alert("Invalid data");
      return;
    }

    //Generate a file name
    var fileName = "Formapelec_";
    //this will remove the blank-spaces from the title and replace it with an underscore
    fileName += ReportTitle.replace(/ /g,"_");

    //Initialize file format you want csv or xls
    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);

    // Now the little tricky part.
    // you can use either>> window.open(uri);
    // but this will not work in some browsers
    // or you will not get the correct file extension

    //this trick will generate a temp <a /> tag
    var link = document.createElement("a");
    link.href = uri;

    //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden";
    link.download = fileName + ".csv";

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

答案 1 :(得分:1)

一般方法是:

  1. 创建将单词映射到替换词的词典
  2. 编译匹配字典中所有键的正则表达式
  3. 匹配正则表达式,并将每个匹配项替换为匹配键的字典值。
  4. 在Perl中,这样的函数看起来像这样:

    sub replace_all {
      my ($string, %replacements) = @_;
      # Assemble the regex. 
      # Sort reverse so that longer keys are matched first.
      # Quotemeta each key in order to escape special characters.
      my $pattern = join '|', map quotemeta, reverse sort keys %replacements;
      $string =~ s/($pattern)/$replacements{$1}/g;  # replace all
      return $string;
    }
    

    测试:

    use Test::More;
    my $output = replace_all(
      "The quick brown fox jumped over the lazy dog.",
      quick => 'slow',
      brown => 'black',
      fox => 'bear');
    my $expected = "The slow black bear jumped over the lazy dog.";
    is $output, $expected;
    done_testing;
    

答案 2 :(得分:1)

在Python中,您可以将替换函数传递给re.sub以返回一个字符串,以用作替代给定匹配项。我们可以先创建一个包含所有模式及其替换的dict,然后编写一个查找函数,使用合适的替换字符串。

import re


def stri_replace_all_fixed(stri, patterns, replacements):
    pattern_to_replacement = {p: r for p, r in zip(patterns, replacements)}

    def switch(match):
        return pattern_to_replacement[match.group(0)]

    # To make sure that longer patterns are matched first
    # we sort the list by the length of its elements and reverse it
    patterns = sorted(patterns, key=len, reverse=True)

    print(re.sub('|'.join(patterns), switch, stri))


pattern = "quick", "brown", "fox"
replace = "slow", "black", "bear"
stri_replace_all_fixed("The quick brown fox jumped over the lazy dog.", pattern, replace)