John Beliecky
John Beliecky

Reputation: 21

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!

Upvotes: 0

Views: 63

Answers (2)

Toto
Toto

Reputation: 91385

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

Upvotes: 1

lufte
lufte

Reputation: 1364

This works with Python3 re module, which uses Perl regex syntax.

import re
>>> re.sub('^(.*)( {.*?};)(.*)$', r'\1\3\2', 'allow { read }; recovery rctl_dumpstate_props:file')
'allow recovery rctl_dumpstate_props:file { read };'

You will probably need to make adjustments for whitespace, but the gist of it is that you capture 3 groups (the prefix, the text between curly braces, and the suffix; and then you swap their positions.

Upvotes: 0

Related Questions