ravi shankar
ravi shankar

Reputation: 3

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.

Upvotes: 0

Views: 99

Answers (3)

SL5net
SL5net

Reputation: 2556

this should work

(\w+)\s*\([^()]*\)

see 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;

result:

function_name1function_name2function_name3function_name4

or output the complete result array:

echo var_export($o[1]);

example in php: http://sandbox.onlinephpfunctions.com/code/b3e387797e12853be040e00fcfd1d804629797b6

Result is:

array (
  0 => 'function_name1',
  1 => 'function_name2',
  2 => 'function_name3',
  3 => 'function_name4',
)

Upvotes: 1

The Scientific Method
The Scientific Method

Reputation: 2436

try this and take group1

(?:\)\s*)(\w+)

demo

Non-capturing group (?:\)\s)

\) matches the character ) literally (case sensitive)

\s matches any whitespace character (equal to [\r\n\t\f\v ])

1st Capturing Group (\w+)

\w+ matches any word character (equal to [a-zA-Z0-9_])

Upvotes: 0

Yanis.F
Yanis.F

Reputation: 684

The any space (space, tab or linebreak) in regex is \s so you can use

\)\s*(.*?)\(

Upvotes: 1

Related Questions