Rearranging text

A place to try and solve your RegEx problems.
Post Reply
User avatar
wherry

Rearranging text

Post by wherry »

Hi
I have the lines similar to ",{$00, $07, $00, $07, $00} // 22 " and wish to change them to "// 22 $00, $07, $00, $07, $00} "
I have tried search term ",{| *(\ // \d\S \S)" to find the lines and have used the replacement term "$1" the result is "$00, $07, $00, $07, $00} // 22 " I expected // 22 $00, $07, $00, $07, $00} // 22 " what am I doibg wrong?
Kind regards Dick
User avatar
Fool4UAnyway

Re: Rearranging text

Post by Fool4UAnyway »

Could you please rearrange your own text?

It would be far more clear what you want(ed) and what you got instead, if you place your matches and results on individual lines.

I have this:
123 456 789

I want this:
321 654 987

I got this:
789 456 123
User avatar
Guest

Re: Rearranging text

Post by Guest »

OK Thanks for the reply, I will try again...............
I have a series of text lines as below
Const Font ROM = $00, $00, $00, $00, $00 // 20
Const Font ROM = $00, $00, $5f, $00, $00 // 21 !
Const Font ROM = $00, $07, $00, $07, $00 // 22 "
etc..
I want
Const // 20 ROM = $00, $00, $00, $00, $00
Const // 21 ROM = $00, $00, $5f, $00, $00
Const // 22 ROM = $00, $07, $00, $07, $00
etc ....

I have tried the regular expression Font( // \d\S)
and replace term $1

but have no matches.
I am trying to search for lines with both the word "Font" and the hex number sequence "// xx" where xx can be any two digit hex number such as "2a", "3b", "49" etc and replace the word "Font" with the second search term.
Any help would be appreciated as the Font table is a lot of lines to change by hand and I would like to know if and how it could be done for future reference.
Kind regards Dick
User avatar
Fool4UAnyway

Re: Rearranging text - that help's a lot!

Post by Fool4UAnyway »

OK, thank you for clarifying. It's very nice that you also describe what you want in terms like "I want to search for this and that ... and move that to this's location".

First of all, you could do this all by hand a little handier: it looks like all the terms on these lines have fixed positions. If you have an editor that supports a column mode or block mode, you might just select the // xx columns on all lines at once and copy - paste them into the Font columns.
You can take a look at Notepad++, for instance, although this program is not slower as the number of lines grow.

But here's the regular expression solution anyway, because that doesn't involve much handwork at all.

Let's keep it strict by requiring the complete "constant" text.

Each line to be processed has to start with "Const Font ROM ="
^Const Font ROM =

Then a series of 5 hexadecimal bytes follow with comma's in-between them and a space character after that last:
( \$[0-9a-f]{2}[, ]){5}

This is a simplified version that allows either a comma or a space character after each hexadecimal byte. If the text is always as shown, this simplification will select the part before // xx.

Then comes the // xx part:
// [0-9a-f]{2}

After that, I see there may be a space character and then the ASCII representation of the hexadecimal byte.
_.$

_ represents the space character, which might get lost on forums if you just type it like it should be. $ means: "end of line", so this would have to be the complete line.

Find:
^Const Font ROM = (( \$[0-9a-f]{2},?){5}) // ([0-9a-f]{2}) .$

Replace by:
Const $2 ROM = $1

I changed the first captured group to exclude the trailing space character, so it won't be re-placed using $1.

By the way, if all your lines have this layout and your files ONLY contain lines of this kind, you could also write a more simple regular expression to achieve the same as the column block mode manual move.

You might have to check and count the actual numbers of characters in your files.

Find:
^(.{6}).{4}(.{30})(.5).2$

Replace by:
$1$3$2

Or like you originally intended, you could use a less strict regular expression that does not even involve counting character positions.

Find:
Font([^/]+) (// [0-9a-f]{2}).*

Replace by:
$2$1
User avatar
Fool4UAnyway

Re: Rearranging text - some corrections

Post by Fool4UAnyway »

> You can take a look at Notepad++, for instance,
> although this program GETS slower as the number of lines grow.

Find:
^Const Font ROM = (( \$[0-9a-f]{2},?){5}) (// [0-9a-f]{2}) .$

Replace by:
Const $2 ROM = $1

! I forgot to include the // in the second captured group.

Const Font ROM = $00, $00, $00, $00, $00 // 20

You might have to check and count the actual numbers of characters in your files.

Find:
^(.{6}).{4}(.{30}).(.5)..$

Replace by:
$1$3$2

! Inserted the dot between the second and third captured group: it is the space character in-between them, which I would not re-place.

! Typed .. to match any pair of characters, instead of .2 which should have been .{2} anyway, but .. is shorter than .{2}.
Post Reply