获取以 '(' - PHP 正则表达式开头的单词
Get words begin with '(' - PHP regex
hy my name is mike(mike)
hi my name is julia (julia)
martin(martin) is 27 years old.
michael (michael) and joseph(joseph) are good friens
我有一行,每个名字都在括号内重复。有的括号与名称统一,有的用白色space分隔。我希望所有括号都用 just 1 whitespace 分隔。就这样。
hy my name is mike (mike)
hi my name is julia (julia)
martin (martin) is 27 years old.
michael (michael) and joseph (joseph) are good friens
我试过了
preg_replace('/\((\S*)\)/', ' ()', $string);
它很好用,但是我想问一个小问题。
它分隔已经分隔的括号名称。因此它们被两个白色spaces
分开
对我来说,这个 ^
正则表达式 /^\((\S*)\)/
应该可以解决问题,但是它给了我错误。感谢帮助
使用前导边界。
\b\((\S*)\)
演示:https://regex101.com/r/qF2kT5/1
PHP:
$string = 'hy my name is mike(mike)
hi my name is julia (julia)
martin(martin) is 27 years old.
michael (michael) and joseph(joseph) are good friens';
echo preg_replace('/\b\((\S*)\)/', ' ()', $string);
输出:
hy my name is mike (mike)
hi my name is julia (julia)
martin (martin) is 27 years old.
michael (michael) and joseph (joseph) are good friens
^
用于字符串或行的开头(如果使用 m
修饰符)。
好吧,您可以使用此正则表达式轻松格式化您的字符串 /(?<!\s)[(]/si
PHP code snippet.
$subject =
"hy my name is mike(mike)
hi my name is julia (julia)
martin(martin) is 27 years old.
michael (michael) and joseph(joseph) are good friens";
$result = preg_replace('/(?<!\s)[(]/si', ' (', $subject);
print($result);
输出:
hy my name is mike (mike)
hi my name is julia (julia)
martin (martin) is 27 years old.
michael (michael) and joseph (joseph) are good friens
hy my name is mike(mike)
hi my name is julia (julia)
martin(martin) is 27 years old.
michael (michael) and joseph(joseph) are good friens
我有一行,每个名字都在括号内重复。有的括号与名称统一,有的用白色space分隔。我希望所有括号都用 just 1 whitespace 分隔。就这样。
hy my name is mike (mike)
hi my name is julia (julia)
martin (martin) is 27 years old.
michael (michael) and joseph (joseph) are good friens
我试过了
preg_replace('/\((\S*)\)/', ' ()', $string);
它很好用,但是我想问一个小问题。
它分隔已经分隔的括号名称。因此它们被两个白色spaces
分开对我来说,这个 ^
正则表达式 /^\((\S*)\)/
应该可以解决问题,但是它给了我错误。感谢帮助
使用前导边界。
\b\((\S*)\)
演示:https://regex101.com/r/qF2kT5/1
PHP:
$string = 'hy my name is mike(mike)
hi my name is julia (julia)
martin(martin) is 27 years old.
michael (michael) and joseph(joseph) are good friens';
echo preg_replace('/\b\((\S*)\)/', ' ()', $string);
输出:
hy my name is mike (mike)
hi my name is julia (julia)
martin (martin) is 27 years old.
michael (michael) and joseph (joseph) are good friens
^
用于字符串或行的开头(如果使用 m
修饰符)。
好吧,您可以使用此正则表达式轻松格式化您的字符串 /(?<!\s)[(]/si
PHP code snippet.
$subject =
"hy my name is mike(mike)
hi my name is julia (julia)
martin(martin) is 27 years old.
michael (michael) and joseph(joseph) are good friens";
$result = preg_replace('/(?<!\s)[(]/si', ' (', $subject);
print($result);
输出:
hy my name is mike (mike)
hi my name is julia (julia)
martin (martin) is 27 years old.
michael (michael) and joseph (joseph) are good friens