Regex to replace function with an other - regex

I am trying to create a regex statement to go through my code and replace a function with an alternative that takes different parameters.
The function I want to replace has this signature:
[Label( "some text", Tooltips.ToolTipName, typeof( someClass ) )]
I would like this to be replaced with.
[Tooltip( Tooltips.ToolTipName )]
Can someone assist in writing a regex statement that will detect the signature and extract the ToolTip variable so I can do this?
Thanks
Karl

You can use this regex:
.*Tooltips\.\w+.*
Working demo

I think this is the regex you'd use to find:
"\\[Label\\([^\\]]*(Tooltips\\.[a-zA-Z0-9_]+)[^\\]]*\\)\\]"
which says "literal bracket and paren followed by label and literal open paren, then possibly anything except the closing bracket, then tooltip, then anything but closing bracket, then close bracket.
Once you've found it, use group(0) to match the entire label, and group(1) to match the "Tooltips.name".
Then replace group(0) with "[Tooltip(" + group(1) + ")]".
You may be able to simplify or be more exact if you are certain of whitespace and that all attributes are single-line or not.

Related

Regex (ICU) for matching between parentheses

Looking for some regex which will create a capture group for words occurring within parentheses, ignoring the parentheses themselves. The regex must be either PCRE or ICU.
Input: ( lakshd asd___ asa1123 Name : _____)
Desired Output: Name
What I've tried:
\\((Name|name|NAME)\\)
(?<=\\()name|Name|NAME(?=\\))
\\(name|Name|NAME\\)
What I've tried:
\\((Name|name|NAME)\\)
(?<=\\()name|Name|NAME(?=\\))
\\(name|Name|NAME\\)
All these patterns look for name or Name or NAME that has a ( immediately before and ) right after, with difference being what is captured or returned as a match. To match some word inside parentheses, you need to use \([^()]* before the value you need to get, and [^()]*\) after it.
Also, there is no point in extracting something you already know.
So, if you plan to extract the last word from the parentheses, you may use
> library(stringr)
> s = "( lakshd asd___ asa1123 Name : _____)"
> res <- str_match(s, "(?i)\\([^()]*\\b([a-z]\\w*)\\b[^()]*\\)")
> res[,2]
[1] "Name"
Note that str_match allows accessing captured values.
The (?i)\\([^()]*\\b([a-z]\\w*)\\b[^()]*\\) pattern matches parentheses and the last whole word from it.
If nested levels of parentheses are not likely to happen then looking if current position is going to be followed by a closing parenthesis at the end while an opening parenthesis is supposed to be opened already will do the trick (works with both ICU and PCRE):
(Name|name|NAME)(?=[^()]*\))
PCRE live demo

How to do regular Expression in AutoIt Script

In Autoit script Iam unable to do Regular expression for the below string Here the numbers will get changed always.
Actual String = _WinWaitActivate("RX_IST2_AM [PID:942564 NPID:10991 SID:498702881] sbivvrwm060.dev.ib.tor.Test.com:30000","")
Here the PID, NPID & SID : will be changing and rest of the things are always constant.
What i have tried below is
_WinWaitActivate("RX_IST2_AM [PID:'([0-9]{1,6})' NPID:'([0-9]{1,5})' SID:'([0-9]{1,9})' sbivvrwm060.dev.ib.tor.Test.com:30000","")
Can someone please help me
As stated in the documentation, you should write the prefix REGEXPTITLE: and surround everything with square brackets, but "escape" all including ones as the dots (.) and spaces () with a backslash (\) and instead of [0-9] you might use \d like "[REGEXPTITLE:RX_IST2_AM\ \[PID:(\d{1,6})\ NPID:(\d{1,5})\ SID:(\d{1,9})\] sbivvrwm060\.dev\.ib\.tor\.Test\.com:30000]" as your parameter for the Win...(...)-Functions.
You can even omit the round brackets ((...)) but keep their content if you don't want to capture the content to process it further like with StringRegExp(...) or StringRegExpReplace(...) - using the _WinWaitActivete(...)-Function it won't make sense anyways as it is only matching and not replacing or returning anything from your regular expression.
According to regex101 both work, with the round brackets and without - you should always use a tool like this site to confirm that your expression is actually working for your input string.
Not familiar with autoit, but remember that regex has to completely match your string to capture results. For example, (goat)s will NOT capture the word goat if your string is goat or goater.
You have forgotten to add a ] in your regex, so your pattern doesn't match the string and capture groups will not be extracted. Also I'm not completely sold on the usage of '. Based on this page, you can do something like StringRegExp(yourstring, 'RX_IST2_AM [PID:([0-9]{1,6}) NPID:([0-9]{1,5}) SID:([0-9]{1,9})]', $STR_REGEXPARRAYGLOBALMATCH) and $1, $2 and $3 would be your results respectively. But maybe your approach works too.

Vim/Perl Regex Tag Match Problem

I have data that looks like this:
[Shift]);[Ctrl][Ctrl+S][Left mouse-click][Backspace][Ctrl]
I want to find all [.*] tags that have the word mouse in them. Keeping in mind non-greedy specifiers, I tried this in Vim: \[.\{-}mouse.\{-}\], but this yielded this result,
[Shift]);[Ctrl][Ctrl+S][Left mouse-click]
Rather than just the desired,
[Left mouse-click]
Any ideas? Ultimately I need this pattern in Perl syntax as well, so if anyone has a solution in Perl that would also be appreciated.
\[[^]]*mouse[^[]*\]
That is, match a literal opening bracket, then any number of characters that aren't closing brackets, then "mouse," then any number of non-opening-brackets, and finally a literal closing bracket. Should be the same in Perl.
You can use the following regex:
\[[^\]]*mouse.*?\]

IntelliJ IDEA: regex for removing Oracle to_timestamp syntax

I'm trying to come up with a search/replace expression that will convert from Oracle style inserts with timestamp fields to insert statements for another database.
Basically, I want to convert strings like:
to_timestamp('13-SEP-09 12.00.00.000000000 PM','DD-MON-RR HH.MI.SS.FF AM')
to just:
'13-SEP-09 12.00.00.000000000 PM'
I've tried several expressions in the IDEA's search/replace box, but I still can't quite get it. This one:
to_timestamp(.[^,]*,.[^)]*)
replaced with $1 ends up matching the string I want except the close parenthesis, but then only deletes the first part. I end up with:
('13-SEP-09 12.00.00.000000000 PM','DD-MON-RR HH.MI.SS.FF AM')
I really don't understand what's happening here.
Looks like you want:
to_timestamp\(('[^']*')[^)]*\)
Breaking it down:
to_timestamp is obvious
\( matches the opening paren
( starts capturing
'[^']' matches the first quoted string
) stops capturing
[^)]*\) matches the remaining text
If that's the whole string you're matching, and not just part of a larger text, you can use .* instead of [^)]*\) for the last part; you don't really care what comes after the closing '.

How do I access matched objects for replacement when using regular expression mode in PL/SQL Developer Find & Replace?

I have a query where I want to replace
avg(j2)
with
avg(case when j2 <> 0 then j2 else 0 end)
The above is a specific example but the pattern is the same with all the replacements. It's always a word followed by a number that needs to be replaced with the case statement that checks if the number is not 0.
I tried the following for find:
avg(\(\w\d\))
and the find works. Now, I want to do a replace so I try:
avg(case when \1 <> 0 then \1 else 0 end)
but it puts literal \1 and not the captured text from the match. I tried \\1 & $1 as well and it takes all of them literally. Can anyone tell me what the right syntax is for using the captured text for replacement? Is this supported?
Thanks,
Ashish
I am not sure if the PL/SQL Developer IDE supports group capture. The recent versions do seem to support regex based find and replace though. Cant find a source to confirm if group capture works.
Why dont you try pasting the code in a something like Notepad++ and try the same regex. It should work. You could paste the result back to your IDE and continue from there...
You can replace it using $ and number like,
$0 or $1 etc. see an example below
find: TABLE (.*\..*) IS
replace: COLUMN $1 IS
http://regexr.com/3gm6c