简体   繁体   English

正则表达式删除尾随字符

[英]Regular expression to remove trailing chars

I'm looking for a regular expression in Php that could transform incoming strings like this: 我正在寻找PHP中的正则表达式,它可以转换这样的传入字符串:

abaisser_negation_pronominal_question     => abaisser_n_p_q
abaisser_pronominal_question              => abaisser_p_q
abaisser_negation_question                => abaisser_n_q
abaisser_negation_pronominal              => abaisser_n_p
abaisser_negation_voix_passive_pronominal => abaisser_n_v_p_p
abaisser                                  => abaisser

With the Php code close to something like: 与PHP代码接近的是:

$line=preg_replace("/<h3>/im", "", $line);

How would you do? 你会怎么做?

You can use: 您可以使用:

$input = preg_replace('/(_[A-Za-z])[^_\n]*/', '$1', $input);

RegEx Demo 正则演示

Explanation: 说明:

  • This regex searches for (_[A-Za-z])[^_\\n]* which means underscore followed by single letter and then match before a newline or underscore 此正则表达式搜索(_[A-Za-z])[^_\\n]* ,表示下划线后跟单个字母,然后在换行符或下划线前匹配
  • It capture first part (_[A-Za-z]) in a backreference $1 它在向后引用$1捕获第一部分(_[A-Za-z])
  • Replacement is $1 leaving underscore and first letter in the replacement string 替换为$1 ,替换字符串中保留下划线和第一个字母
$line = preg_replace("/_([a-z])([a-z]*)/i", "_$1", $line);

You can use regex 您可以使用正则表达式

$input = preg_replace('/_(.)[^\n_]+/', '_$1', $input);

DEMO DEMO

What it does is capture the character after _ and match till \\n or _ is encountered and replaced with the _$1 which means _ plus the character captured. 它的作用是捕获_之后的字符并匹配,直到遇到\\n_为止,然后替换为_$1 ,这意味着_加上捕获的字符。

You could use \\K or positive lookbehind. 您可以使用\\K或正向后看。

$input = preg_replace('~_.\K[^_\n]*~', '', $input);

Pattern _. 模式_. in the above regex would match an _ and also the character following the underscore. 上面的正则表达式中的_和下划线后的字符都匹配。 \\K discards the previously matched characters that is, _ plus the following character. \\K丢弃先前匹配的字符,即_和后续字符。 It won't take these two characters into consideration. 不会考虑这两个字符。 Now [^_\\n]* matches any character but not of an _ or a \\n newline character zero or more times. 现在[^_\\n]*匹配任何字符,但不匹配_\\n换行符零次或多次。 So the characters after the character which was preceded by an underscore would be matched upto the next _ or \\n character. 因此,在下划线之前的字符之后的字符将与下一个_\\n字符匹配。 Removing those characters will give you the desired output. 删除这些字符将为您提供所需的输出。

DEMO DEMO

$input = preg_replace('~(?<=_.)[^_\n]*~', '', $input);

It just looks after to the _ and the character following the _ and matches all the characters upto the next underscore or newline character. 它只是看起来到后_和字符后的_和高达下一个下划线或换行符的所有字符匹配。

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM