Page 1 of 1

Question about regular expressions

Posted: 30 Apr 2025 17:25
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???

Re: Question about regular expressions

Posted: 30 Apr 2025 17:58
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\("(.*)"\);$

Re: Question about regular expressions

Posted: 30 Apr 2025 19:06
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 ?!?!

Re: Question about regular expressions

Posted: 30 Apr 2025 21:19
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).

Re: Question about regular expressions

Posted: 30 Apr 2025 23:20
by DerellLicht1
Excellent!! I will do so; thank you again!