I am using regular expressions in Eclipse IDE. I am trying to turn
other.test into other.getTest()
Search: other.([a-z])([a-z]*)
Replace: other.\U$1$2()
Result: other.Utest()
I thought that adding a \U in front of the group was supposed to change the case, but its not working for me. any ideas?
Unfortunately, Eclipse Find/Replace regex does not support case modifying operators like \U, \u, \L and \l. You may either use a long workaround suggested by jrahhali, or use Notepad++:
Search: other\.([a-z]+)
Replace: other.get\u$1\(\)
Explantion:
other\. - matches a string other. (note the dot must be escaped to match a literal dot)
([a-z]+) - Group 1 capturing 1 or more lowercase ASCII letters (check Match case option to only match lowercase ASCII letters with [a-z]+)
Replacement pattern details:
other.get - a literal text other.get
\u$1 - the contents of Group 1 (the $1 is a backreference to the captured group 1) and its first character is turned to upper case with \u operator (\U would turn the whole text of the capture group to upper case)
\(\) - a literal text () (the parentheses should be escaped in NPP Boost conditional replacement patterns).
Demo screen:
This works. You need two passes with search and replace. Reference from this answer: Is it possible to transform to lowercase using Eclipse's regex search and replace?
search 1: other\.([a-z])([a-z]*)
replace 1: other.ABCDEFGHIJKLMNOPQRSTUVWXYZ$1$2
search 2: other\.(A)BCDEFGHIJKLMNOPQRSTUVWXYZa|A(B)CDEFGHIJKLMNOPQRSTUVWXYZb|AB(C)DEFGHIJKLMNOPQRSTUVWXYZc|ABC(D)EFGHIJKLMNOPQRSTUVWXYZd|ABCD(E)FGHIJKLMNOPQRSTUVWXYZe|ABCDE(F)GHIJKLMNOPQRSTUVWXYZf|ABCDEF(G)HIJKLMNOPQRSTUVWXYZg|ABCDEFG(H)IJKLMNOPQRSTUVWXYZh|ABCDEFGH(I)JKLMNOPQRSTUVWXYZi|ABCDEFGHI(J)KLMNOPQRSTUVWXYZj|ABCDEFGHIJ(K)LMNOPQRSTUVWXYZk|ABCDEFGHIJK(L)MNOPQRSTUVWXYZl|ABCDEFGHIJKL(M)NOPQRSTUVWXYZm|ABCDEFGHIJKLM(N)OPQRSTUVWXYZn|ABCDEFGHIJKLMN(O)PQRSTUVWXYZo|ABCDEFGHIJKLMNO(P)QRSTUVWXYZp|ABCDEFGHIJKLMNOP(Q)RSTUVWXYZq|ABCDEFGHIJKLMNOPQ(R)STUVWXYZr|ABCDEFGHIJKLMNOPQR(S)TUVWXYZs|ABCDEFGHIJKLMNOPQRS(T)UVWXYZt|ABCDEFGHIJKLMNOPQRST(U)VWXYZu|ABCDEFGHIJKLMNOPQRSTU(V)WXYZv|ABCDEFGHIJKLMNOPQRSTUV(W)XYZw|ABCDEFGHIJKLMNOPQRSTUVW(X)YZx|ABCDEFGHIJKLMNOPQRSTUVWX(Y)Zy|ABCDEFGHIJKLMNOPQRSTUVWXY(Z)z([a-z]*)
replace 2: other.get$1$2$3$4$5$6$7$8$9$10$11$12$13$14$15$16$17$18$19$20$21$22$23$24$25$26$27
simple one.. according to http://www.regexe.com/ it works fine:
search pattern - other\.t(est)
replace pattern - other\.getTest\(\)
Good luck..
Related
In the context of an XML file, I want to use the XML tags in a positive look-behind and positive look-ahead to convert a value to lowercase.
BEFORE:
<CONDITION NAME="ABC-DEF-GHI" DATE="DATE">
AFTER:
<CONDITION NAME="abc-def-ghi" DATE="DATE">
Pattern's tried from other questions/regex wiki that don't work.
1.
FIND:
(?<=(<CONDITION NAME="))(.+)(?=(" DATE="DATE"))
REPLACE:
\L0
FIND:
(?<=(<CONDITION NAME=".*))(\w)(?=(.*" DATE="DATE"))
REPLACE:
\L$1
Using VS Code 1.62.1
MAC OS Darwin x64 19.6.0
You don't need any capture groups if yo want to use lookarounds at the left and right side.
Instead of using .+ which is a broad match and can match too much, you can use a negated character class [^"]+ to match any character except a double quote, or you can use [\w-]+ to match 1 or more word characters or a hyphen:
(?<=<CONDITION NAME=")[^"]+(?=" DATE="DATE")
Regex demo
Replace with the full match using $0
\L$0
Another option is to use 2 capture groups with a single lookahead as lookarounds can be expensive, and replace with $1\L$2
(<CONDITION NAME=")([\w-]+)(?=" DATE="DATE")
Pattern 2 works. The replace value just needs to change from
\L$1 -> \L$2
Pattern 1 could also be used with \L$2 as the replace value.
This pattern works:
FIND:
(?<=(<CONDITION NAME=".*))(\w)(?=(.*" DATE="DATE"))
REPLACE:
\L$2
Make sure you make the other groups non-capturing:
(?<=(?:<CONDITION NAME="))(.+)(?=(?:" DATE="DATE"))
Or leave out the inner () altogether:
(?<=<CONDITION NAME=")(.+)(?=" DATE="DATE")
Or use $2 as replacement. Everything between standard () becomes a captured group, no matter where in the expression they are.
And be careful with .+, in this case [^"]+ is a much safer choice.
I need to do some re-factoring on my Java code. I need to turn this:
X.format("Z")
into this:
(new SimpleDateFormat("Z").format(X))
Examples:
dateStart.format("yyyy-MM-dd HH:mm") into
(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(dateStart))
reportStart.format("yyyy-MM") into
(new SimpleDateFormat("yyyy-MM").format(reportStart))
I'm thinking to use Notepad++ find/replace, but I'm not good with Regex, and hoping someone would know easily?
I've tried variations of the below, and the closest I've got is with the one below... But with the one below, it wants to take everything to the left of .format and treat that as $1
find:([^)]*)\.format\(([^)]*)\) replace with:
(new SimpleDateFormat($2.format($1))
Probably a simple find / replace will work like this :
Find (?s)(\w+)\.format\((.*?)\)
update Escape the parenthesis when used as literals because Boost::regex uses these characters as special operators in the replacement, format string.
Boost-Extended format strings treat all characters as literals except for '$', '\', '(', ')', '?', and ':'
Replace \(new SimpleDateFormat\($2\).format\($1\)\)
https://regex101.com/r/f77yBt/1
If interested in why certain characters need to be escaped to be considered
literals, see this :
https://www.boost.org/doc/libs/1_70_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
Essentially, boost::regex uses these characters to implement a pseudo-callback
that does simple (possibly nested) conditionals checking if a group matched
and taking a yes : no replacement action.
Be aware that in Notepad++ the parenthesis have to be escaped in the replacement part.
Ctrl+H
Find what: (\w+)\.format\((.+?)\)
Replace with: \(new SimpleDateFormat\($2\).format\($1\)\)
CHECK Match case
CHECK Wrap around
CHECK Regular expression
UNCHECK . matches newline
Replace all
Explanation:
(\w+) # group 1, 1 or more word characters
\. # a dot
format\( # literally
(.+?) # group 2, 1 or more any character but newline, not greedy
\)
Screen capture (before):
Screen capture (after):
Consider the following list of words:
targetX
targetY
targetZ
targetColor
targetWidth
I want to remove the "target" prefix, while maintaining the naming convention. This means that the first character after the prefix needs to be downcased:
x
y
z
color
width
So far I can match the text that I want and group the characters accordingly:
Find: "target[A-Z]"
Replace: ???
But I don't know what I can do for the replace. Is there a regex substitution function that can do this in Visual Studio, or an alternative editor?
Visual Studio S&R tool does not support upper-/lowercasing operators in the replacement part. The work around is to use Notepad++ or SublimeText with
Find: \btarget([A-Z])
Replace with: \l$1
Here, \btarget will match a word starting with target and ([A-Z]) will match and capture an uppercase ASCII letter into Group 1 that will later refernce with a $1 backreference and turn it lowercase with \l operator.
I need to use a Regular Expression in SublimeText for a Find and Replace search.
My search string pattern looks like this:
chrome.i18n.getMessage("nimbusBtnLogin")
in which case I need to replace with:
"nimbusBtnLogin"
I have a GIST here https://gist.github.com/jasondavis/fda85e808a6c8184adad where I have listed the RegEx for a find and replace of HTML form selection option fields, links, and images however I was unable to modify and get working for this pattern above.
Can someone please share the correct RegEx?
You can use
\bchrome\.i18n\.getMessage\("([^"]*)"\)
Replace with $1. See the regex demo
Note:
\bchrome\.i18n\.getMessage\(" - matches literal string (matched as a whole word) chrome.i18n.getMessage(" (special characters are escaped since they must be treated as literals)
([^"]*) - matches and captures into Group 1 any characters other than "
"\) - matches literal ")
I have many source code lines in Lua (a script language) similar to
object:method(...)
Later I decide to make the method a general function, then I need to change the above lines to
method(object,...)
I think a regular expression replace can solve my problem, but I just struggled.
BTW, the name of object and method is following common identity convention, say consists of characters, numbers and "_" only.
Please help. It is better to use an text editor, such as Visual Studio, to do so.
Thank you in advance,
Ying
You can use a simple search and replace:
\b(\w+):(\w+\s*)\(
And replace with $2($1, .
Explanation:
\b - word boundary
(\w+) - Group 1: one or more word characters (in the replacement, this text is backreferenced with $1)
: - a literal :
(\w+\s*) - Group 2: 1 or more word characters followed with zero or more whitespace (in the replacement, this text is backreferenced with $2)
\( - a literal ( character
See regex demo
This expression will work in Notepad++ or Visual Studio.
Here is a Lua solution:
S=[[
object1:method1()
object2:method2(a)
object3:method3(a,b)
]]
S=S:gsub("(%w+):(%w+)%(%)","%2:(%1)")
S=S:gsub("(%w+):(%w+)%((.-)%)","%2:(%1,%3)")
print(S)