Search code examples
phpregexpcre

Need Help to Move a pattern to End Of Line


I've been making auto logcat to sepolicy code with regex on HTML page.

My problem is that i need to move the { .* }; to end of line and i couldn't find how to do that.

Some examples:
allow { read }; recovery rctl_dumpstate_props:file
where the { read }; needs to go to end of line.

allow { getattr }; recovery rcgroups:file
where the { getattr }; needs to go to end of line.

I've tried multiple ReGex from other similar problems from other people like ^(.*)( {.*?};)(.*)$ and replace \1\3\2 but i get output like this (weird squares):

instead of the code i need. I need it to be PCRE (PHP). Thanks!


Solution

  • I guess you are not using properly preg_replace. Have a try with:

    $texts = ['allow { read }; recovery rctl_dumpstate_props:file',
              'allow { getattr }; recovery rcgroups:file'];
    foreach ($texts as $str) {
        echo preg_replace('/^(.+?)( { .+? };)(.+)$/', '$1$3$2', $str),"\n";
    }
    

    Output:

    allow recovery rctl_dumpstate_props:file { read }; 
    allow recovery rcgroups:file { getattr };