[英]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);
Explanation: 说明:
(_[A-Za-z])[^_\\n]*
which means underscore followed by single letter and then match before a newline or underscore (_[A-Za-z])[^_\\n]*
,表示下划线后跟单个字母,然后在换行符或下划线前匹配 (_[A-Za-z])
in a backreference $1
$1
捕获第一部分(_[A-Za-z])
$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);
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. 删除这些字符将为您提供所需的输出。
$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. 它只是看起来到后
_
和字符后的_
和高达下一个下划线或换行符的所有字符匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.