Regular Expression

Look-around assertions:
(?<=expression) = positive look-behind assertion
(?=expression)  = positive look-ahead assertion

\w              = word character [a-zA-Z_0-9]
\s              = space character

List of single characters

File:
hat
tut
cat
blue
red
zat
bet
tat
met
mat

RegEx:
[htm]at => matches "hat", "tat", "mat"

RegEx:
[^htm]at => matches ".at" except "hat", "tat", "mat"
Result: "cat", "zat"

Whitespace at the beginning or end of a line

^[ \t]+|[ \t]+$

Explanation:
^   Start of line
[ ] List of single characters: Space and Tab
+   One or more occurrences
|   OR
$   End of line

Remove newlines

File:
aaa
bbb
ccc

RegEx: '(?<=^.+)\n'

Result:
aaabbbccc

Cut out only define name

#define DEFAULT_CLOCK_BLA                      MAC_ADD_HEAD_1,0xff,0xfe,MAC_ADD_HEAD_2,SR_NR

Remove #define:

DEFAULT_CLOCK_BLA                      MAC_ADD_HEAD_1,0xff,0xfe,MAC_ADD_HEAD_2,SR_NR
Search:  '(?<=(^[\w]+))[\s]*[\w\,]*(?=($))'
Replace: ''
DEFAULT_CLOCK_BLA

Append parameter list

default_clock_bla
Search:  '(?<=(^[\w]+))\s*(?=($))'
Replace: '(void);'
default_clock_bla(void);

Trim to only have one space

bool                            default_clock_bla(void);
Search:  '(?<=(^[\w]+))\s+'
Replace: ' '
bool default_clock_bla(void);

Separate result type from function name

bool default_clock_bla(void);
Search:  '(?<=(^[\w]+))\s'
Replace: '\n'
bool
default_clock_bla(void);

Add curly brackets

bool
default_clock_bla(void);
Search:  ';(?=($))'
Replace: '\n{\n    \n}\n'
bool
default_clock_bla(void)
{
    
}

Add to every line

DEVICE_I2CSLAVE=1
TARGET_LIKE_MBED
DEVICE_PORTOUT=1
DEVICE_PORTINOUT=1
TARGET_RTOS_M4_M7
Search:  '(?<=.*)^'
Replace: '<name>'

Search:  '=1$'
Replace: '</name><value>1</value>'
<name>DEVICE_I2CSLAVE</name><value>1</value>
<name>TARGET_LIKE_MBED
<name>DEVICE_PORTOUT</name><value>1</value>
<name>DEVICE_PORTINOUT</name><value>1</value>
<name>TARGET_RTOS_M4_M7

Add suffix if line-end is NOT “>”

<name>DEVICE_I2CSLAVE</name><value>1</value>
<name>TARGET_LIKE_MBED
<name>DEVICE_PORTOUT</name><value>1</value>
<name>DEVICE_PORTINOUT</name><value>1</value>
<name>TARGET_RTOS_M4_M7
Search:  '(?<=[^>]$)'
Replace: '</name><value/>'
<name>DEVICE_I2CSLAVE</name><value>1</value>
<name>TARGET_LIKE_MBED</name><value/>
<name>DEVICE_PORTOUT</name><value>1</value>
<name>DEVICE_PORTINOUT</name><value>1</value>
<name>TARGET_RTOS_M4_M7</name><value/>

Embrace lines

<name>DEVICE_I2CSLAVE</name><value>1</value>
<name>TARGET_LIKE_MBED</name><value/>
<name>DEVICE_PORTOUT</name><value>1</value>
<name>DEVICE_PORTINOUT</name><value>1</value>
<name>TARGET_RTOS_M4_M7</name><value/>
Search:  '^(?=<name>.*)'
Replace: '<macro>\n'

Search:  '(?<=.*</value>|<value/>)'
Replace: '\n</macro>'
<macro>
<name>DEVICE_I2CSLAVE</name><value>1</value>
</macro>
<macro>
<name>TARGET_LIKE_MBED</name><value/>
</macro>
<macro>
<name>DEVICE_PORTOUT</name><value>1</value>
</macro>
<macro>
<name>DEVICE_PORTINOUT</name><value>1</value>
</macro>
<macro>
<name>TARGET_RTOS_M4_M7</name><value/>
</macro>

One thought on “Regular Expression

  1. Pingback: Eclipse Embedded for ARM | Andreas' Blog

Leave a Reply

Your email address will not be published. Required fields are marked *