簡體   English   中英

Google 表格:REGEXREPLACE 匹配除特定模式之外的所有內容

[英]Google sheet : REGEXREPLACE match everything except a particular pattern

我會嘗試替換此字符串中的所有內容:

[JGMORGAN - BANK2] n° 10 NEWYORK, n° 222 CAEN, MONTELLIER, VANNES / TARARTA TIs 1303222074, 1403281851 & 1307239335 et Cloture TIs 1467031752

以下號碼除外:1303222074 1403281851 1307239335 1403277567 1410315029

我已經建立了一個正則表達式來匹配它們:

1[0-9]{9}

但我還沒有想出來做相反的事情,除了所有比賽......

谷歌電子表格使用 Re2 正則表達式引擎,並且不支持許多可以幫助您做到這一點的有用功能。 因此,一個基本的解決方法可以幫助您:

匹配您要首先保留的內容並捕獲它:

模式: [0-9]*(?:[0-9]{0,9}[^0-9]+)*(?:([0-9]{9,})|[0-9]*\\z)

替換: $1 (后面有一個空格)

演示

所以大概是這樣的:

=TRIM(REGEXREPLACE("[JGMORGAN - BANK2] n° 10 NEWYORK, n° 222 CAEN, MONTELLIER, VANNES / TARARTA TIs 1303222074, 1403281851 & 1307239335 et Cloture TIs 1403277567, 1410315029"; "[0-9]*(?:[0-9]{0,9}[^0-9]+)*(?:([0-9]{9,})|[0-9]*\z)"; "$1 "))

您還可以使用動態本機函數執行此操作:

=REGEXEXTRACT(A1,rept("(\d{10}).*",counta(split(regexreplace(A1,"\d{10}","@"),"@"))-1))

基本上它首先被所需的字符串分割,以確定它出現了多少次,然后重復正則表達式以動態創建該數量的捕獲組,從而最終只留下這些值。

在此處輸入圖片說明

首先感謝卡西米爾的幫助。 它給了我一個想法,這是內置函數和強大的正則表達式無法實現的,哈哈。 我發現我可以為自己的目的制作一個自制函數(是的,我不是很“最新”)。 它的編碼不是很好,它返回doublons 但是我沒有正確修復它,而是在 if 之上使用內置的UNIQUE()函數來擺脫它們; 它很丑,我很懶,但它可以完成工作,即特定正則表達式(即: 1[0-9]{9} )的所有匹配項的列表。 這里是:

function ti_extract(input) {
  var tab_tis = new Array();
  var tab_strings = new Array();


  tab_tis.push(input.match(/1[0-9]{9}/)); // get the TI and insert in tab_tis

  var string_modif = input.replace(tab_tis[0], " "); // modify source string (remove everything except the TI)
  tab_strings.push(string_modif); // insert this new string in the table

  var v = 0;
  var patt = new RegExp(/1[0-9]{9}/);
  var fin = patt.test(tab_strings[v]);

  var first_string = tab_strings[v];


  do {
    first_string = tab_strings[v]; // string 0, or the string with the first removed TI
    tab_tis.push(first_string.match(/1[0-9]{9}/)); // analyze the string and get the new TI to put it in the table

    var string_modif2 = first_string.replace(tab_tis[v], " "); // modify the string again to remove the new TI from the old string
    tab_strings.push(string_modif2);

    v += 1;
  }
  while(v < 15)

  return tab_tis;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM