Question about regular expressions

Ask how to do things or if a certain feature is available in the program.
Post Reply
User avatar
DerellLicht1
Posts: 161
Joined: 23 Jul 2021 17:38
Location: Fremont, CA, USA

Question about regular expressions

Post by DerellLicht1 »

This is rather embarrassing to admit... I've been using Unix-style regular expressions for decades now, but when it comes to search-and-replace operations, I've always been a little uncertain about formatting... I wonder if anyone could advise me here?? This is specifically in the context of S&R in RJTE...

In my ongoing UNICODE conversions, I have countless lines of the form:

Code: Select all

put_message("this is a sample message");
which need to be converted to:

Code: Select all

put_message(_T("this is a sample message"));
I've been doing these changes in two parts; first convert

Code: Select all

("
to

Code: Select all

(_T("
, then convert

Code: Select all

") ;
to

Code: Select all

")) ;
with regex disabled... this works, but I'm hesitant to do a global change because I worry about unintended consequences...

What I would like to do is convert the entire line in one regex operation, but I'm really unsure of the format;
probably it is something like:

source:

Code: Select all

put_message("[*]");
dest:

Code: Select all

put_message(_T("[*]"));
I know a *lot* of escaping of characters will probably be required, but I don't know details...

Could somebody advise on this???
User avatar
pjj
Posts: 2236
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Question about regular expressions

Post by pjj »

Please try

Code: Select all

put_message\("(.*)"\);
put_message(T_("$1"));
You can use first line with Find and mark "Highlight all items found" just to make sure everything is as expected.

If you want to convert lines that have only this command (with variable message), you can use

Code: Select all

^put_message\("(.*)"\);$
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
User avatar
DerellLicht1
Posts: 161
Joined: 23 Jul 2021 17:38
Location: Fremont, CA, USA

Re: Question about regular expressions

Post by DerellLicht1 »

Oh My!!! That is so cool!!!!
Thank ye once again, @pjj !!! That works beautifully!!

I'll save this for future references...

I was even able to project a more-complex example successfully:

put_color_msg(TERM_RUNESTAFF, "You just found *** THE ORB OF ZOT *** !");
(where the TERM_RUNESTAFF could be any constant):

put_color_msg\((.*), "(.*)"
replace with
put_color_msg($1, _T("$2")

And this works perfectly !!! how cool is that ?!?!
User avatar
pjj
Posts: 2236
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: Question about regular expressions

Post by pjj »

Hey, I'm super glad it worked for you :-D

You may want to have a look at https://regexr.com/ or https://regex101.com/ to test your regexes -- the are more regex testers on the Net, but these two come with broad explanations and suggestions, like e.g. to use ([A-Z_]*) instead of (.*) to match only CAPITAL letters and an underscore instead of any character (dot).
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
User avatar
DerellLicht1
Posts: 161
Joined: 23 Jul 2021 17:38
Location: Fremont, CA, USA

Re: Question about regular expressions

Post by DerellLicht1 »

Excellent!! I will do so; thank you again!
Post Reply