Regex:如何在非英语字符串和非英语字符串之间替换符号


Regex: How to replace a symbol between Non-English String but not English one

我有一个字符串,其中包含英文和非英文字符,并且它们之间都包含underscore symbol _

我只想从非英文字符串中删除下划线,只保留英文字符之间的下划线。

示例:Hello_this is me,مرحبا_انا

谢谢!

此preg_replace可能有效:

preg_replace('/(?<=[^'x00-'x80])_(?=[^'x00-'x80])/', '', $str);

这应该可以实现您想要的:

preg_replace('/(?<='p{Arabic})_(?='p{Arabic})/', '', $s)

结账http://www.php.net/manual/en/regexp.reference.unicode.php

你可以试试这个:

<meta charset="UTF-8"/>
<pre><?php
$subject = "Hello_this is me , مرحبا_اناهنا";
$pattern = '~'p{Arabic}'K_(?='p{Arabic})~u';
echo $subject . '<br/>' . preg_replace($pattern, ' ', $subject);

请注意,我使用u修饰符来处理unicode字符串。

'K在比赛之前重置了比赛,但你可以像anubhava和ctn建议的那样,用一个后备来代替这个技巧。