2016-05-23 76 views
-4

該字符串正則表達式匹配和刪除串像`顏色{*}`在PHP

\[\color{Blue}{4} \times 2 \times 5 \times \color{Blue}{25} = \underbrace{\color{Blue}{4} \times \color{Blue}{25}}_{100} \times 5 \times 2 = \underbrace{100 \times 5}_{500} \times 2 = 500 \times 2 = 1\ 000\] 

我想匹配和刪除像\color{*}串在PHP

\color和下一{}及其內容:

  • \顏色{藍}
  • \顏色{紅}

我覺得我不是很遠...... https://regex101.com/r/gM1uW3/1

+2

什麼是你的正則表達式的味道? –

+1

嘗試** [此](https://regex101.com/r/gM1uW3/2)** – rock321987

+0

您的預期結果字符串是什麼? –

回答

2

如果你想刪除所有\color命令(這是Latex?),你可以相處:

\\color\S+ 
# look for \\color literally 
# followed by at least one (but unlimited times) 
# a non-whitespace character 

查看您的modified demo on regex101.com

0

OP的解決方案。

找到解決方案:

preg_match_all('/\\\color\{(.*?)\}/', $latex, $matches); 

if (!empty($matches[0])) { 
    foreach ($matches[0] as $r) { 
     $latex = str_replace($r, '', $latex); 
    } 
}