regex - Remove whitespace in regex match
问题描述
I am using regex to read all the function names from the contract. Example
(void) function_name1(int, int);
(void) function_name2(int, int);
(void) function_name3(int, int);
(void) function_name4(int, int);
output expected:
function_name1
function_name2
function_name3
function_name4
I am using Regex "\)(.*?)\("
With this expression i am grouping the function name with space in begining of the function.
Kindly help me how to ignore this space.
解决方案
这应该工作
(\w+)\s*\([^()]*\)
见https://regex101.com/r/CzjFWt/1
<?php
$i = '(void) function_name1(int, int);
(void) function_name2(int, int);
(void) function_name3(int, int);
(void) function_name4(int, int);';
$r = '(\w_+)\s*\([^()]*\)';
preg_match_all('|' . $r . '|sm',
$i,
$o, PREG_PATTERN_ORDER);
foreach($o[1] as $v)echo $v;
结果:
function_name1function_name2function_name3function_name4
或输出完整的结果数组:
echo var_export($o[1]);
php 中的示例:http: //sandbox.onlinephpfunctions.com/code/b3e387797e12853be040e00fcfd1d804629797b6
结果是:
array (
0 => 'function_name1',
1 => 'function_name2',
2 => 'function_name3',
3 => 'function_name4',
)
推荐阅读
- powershell - 如何通过 Powershell 在 Azure Devops 中生成构建工件
- sql-server - 如何在 SQL Server 中添加前导零
- python - JSON数据的POST与curl的请求不同
- android - 为什么 Android 的“不是以下字符”正则表达式是 [!x] 而不是 [^x]?
- laravel - git push 后cmd似乎坏了,如何修复clink.lua?
- datetime - Dart - 将时间从 dd/MM/YYYY 转换为 YYYY-MM-dd
- r - degree_distribution 向量间隔为 0 每隔一个数字
- oracle - 如何使 Oracle 查询排序顺序动态化?
- c++ - 如何强制 Windows.h 在 Visual Studio 中静态链接而不是动态链接?
- google-apps-script - Telegram Bot webhook 不适用于 Apps 脚本