【问题标题】:Regex PHP, remove chars between 2 characters [duplicate]正则表达式 PHP,删除 2 个字符之间的字符 [重复]
【发布时间】:2017-04-20 05:26:59
【问题描述】:

我想删除 /* 和 */ 之间的字符

我尝试了正则表达式,我认为这很简单,但我对此很不满意:


示例 1:

对于这个字符串:

"/*当 (aa.kbqeben 为 null 且 (aa.KBICBN 为 null 或 aa.KBICBN='NOTPROVIDED') then '******TODO******' */else 'ok ' end nature_trans,"

我只想保留:“else 'interbancaire' end nature_trans”,

示例 2:

对于这个字符串:

"来自 prod.TbSepemi /*partition(P_TBSEPEMI_201601) */ aa,"

我只想保留:“来自 prod.TbSepemi aa”,


我试过了,但它不起作用:

$string = "/*when (aa.kbqeben is null and (aa.KBICBN is null or aa.KBICBN='NOTPROVIDED') then '******TODO******' */else 'interbancaire' end nature_trans,";
    $string = preg_replace('/\/*[^\]]+\*\//', '', $string);
    echo $string;

【问题讨论】:

  • * 在每个实例中都需要转义,否则它将用作量词。例如\/* 是零个或多个正斜杠。为什么要找非]?

标签: php regex


【解决方案1】:

Regex101

\/\*.*\*\/

说明

\/ matches the character / literally (case sensitive)
\* matches the character * literally (case sensitive)
.* matches any character (except for line terminators)
    * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\* matches the character * literally (case sensitive)
\/ matches the character / literally (case sensitive)
Global pattern flags
    g modifier: global. All matches (don't return after first match)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2022-11-17
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多