替换正则表达式捕获没有例外字符串的文本

时间:2022-02-09 09:57:45

I have a problem. i'm programing a ansi to utf-8 format converter for php, css, js files and using windows10 powershell script console. please see my example code:

我有个问题。我正在为php,css,js文件和使用windows10 powershell脚本控制台编程ansi到utf-8格式转换器。请看我的示例代码:

$replacer ="function( testing regex function( bla bla newfunction( alternative function ( not capture this mb_function( or mb_function ( need a right regex."
$regx = "([^_]function\s?\()+"
$replacer -replace $regx, "hot_function("

output(not valid) :

输出(无效):

function( testing regexhot_function( bla bla nehot_function( alternativehot_function( not capture this mb_function( or mb_function ( need a right regex.

need valid output :

需要有效输出:

hot_function( testing regex hot_function( bla bla newhot_function( alternative hot_function( not capture this mb_function( or mb_function ( need a right regex.

i'm testing regex this page not found right syntax : https://regex101.com/r/kV4gV6/1 how to fix this problem please help me. Thank you all helpers.

我正在测试正则表达式这个页面找不到正确的语法:https://regex101.com/r/kV4gV6/1如何解决这个问题请帮帮我。谢谢大家的帮助。

1 个解决方案

#1


2  

I'd use a negative lookbehind in this case:

在这种情况下,我会使用负面的lookbehind:

(?<!_)function\s*\(
^^^^^^

It will fail the match if an f is preceded with _.

如果f前面带_,它将失败。

See the regex demo

请参阅正则表达式演示

Replace with hot_function(:

替换为hot_function(:

替换正则表达式捕获没有例外字符串的文本

#1


2  

I'd use a negative lookbehind in this case:

在这种情况下,我会使用负面的lookbehind:

(?<!_)function\s*\(
^^^^^^

It will fail the match if an f is preceded with _.

如果f前面带_,它将失败。

See the regex demo

请参阅正则表达式演示

Replace with hot_function(:

替换为hot_function(:

替换正则表达式捕获没有例外字符串的文本