d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Regex Find/replace Question
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2014 01:08pm
Working on a javascript project using titanium studio (forked from aptana).

we have a utility class jutils, and in all our code i have this:

Code
var j = require('jutils');


bunch of utility functions

j.parseInt(...);
if (j.isnull(..))...

etc

Coworker wants to rename it from j to JUtils. the IDE doesn't offer a good way to do that. is there a good way to do that via regex?

so change things like this:
j.parseInt(..) => JUtils.parseInt(..)
if (j.isnull(..)) => if (JUtils.isnull(..))
thisObj.doSomething() => no change

i can use the regex [^a-zA-Z]j. to find it. the problem is if i do a replace on it, it will replace the character immediately to the left of j, eg:

if (j.isnull(..)) => if JUtils.isnull(..))

so i need the find/replace to recognize that it's not immediately following a letter, but it has to keep whatever character is there.

any suggestions? i can write a quick java app to do it or run a bunch of different find/replace for (j [j <space>j <tab>j *j +j -j etc etc, but i'm curious if it can be done via a single find/replace
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Dec 12 2014 01:22pm
Code
find ./ -type -f name *.js -exec sed -i 's/j\./jUtils\./g' '{}' \;


finds all files ending in .js then replaces any j. substrings with jUtils.

This post was edited by SelfTaught on Dec 12 2014 01:25pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2014 06:07pm
Quote (SelfTaught @ Dec 12 2014 02:22pm)
Code
find ./ -type -f name *.js -exec sed -i 's/j\./jUtils\./g' '{}' \;


finds all files ending in .js then replaces any j. substrings with jUtils.


is that going to replace (j.isnull) while leaving thisObj.isnull alone?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Dec 12 2014 06:14pm


edit nvm - same problem, lulz

(testing em on rubular lol)

This post was edited by Eep on Dec 12 2014 06:18pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2014 06:33pm
well, the root of the problem is that regex matches a text, and the replace will replace the exact match. i need the replace to "remember" part of the match and not mess with it. maybe i need a double-regex? one regex to find what i want, then a second regex to specify what part of it i'm replacing from the match prior.
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Dec 12 2014 06:46pm
Code
pathToFile = 'something.js'

lines = IO.readlines(pathToFile).map do |line|
line = line.gsub(/\b(j.)/, "JUtils.")
end

File.open(pathToFile, "w") do |file|
file.puts(lines)
end



Before:
Code
j.parseInt(..) => JUtils.parseInt(..)
if (j.isnull(..)) => if (JUtils.isnull(..))
thisObj.doSomething() => no change


After:
Code
JUtils.parseInt(..) => JUtils.parseInt(..)
if (JUtils.isnull(..)) => if (JUtils.isnull(..))
thisObj.doSomething() => no change


This post was edited by killg0re on Dec 12 2014 06:47pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Dec 12 2014 06:55pm
Maybe I am just misunderstanding your problem. But if you are losing the character to the immediate left, then just capture it.

(\W*)j.(.*)

and then when you replace just do

$1JUtils.$2
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2014 07:06pm
Quote (Minkomonster @ Dec 12 2014 07:55pm)
Maybe I am just misunderstanding your problem. But if you are losing the character to the immediate left, then just capture it.

(\W*)j.(.*)

and then when you replace just do

$1JUtils.$2


never used $i groups before. that's what i needed :thumbsup:

is that standard across most apps?
Quote

Code
pathToFile = 'something.js'

lines = IO.readlines(pathToFile).map do |line|
  line = line.gsub(/\b(j.)/, "JUtils.")
end

File.open(pathToFile, "w") do |file|
  file.puts(lines)
end


what is that, ruby? i'd prolly stick with a language i'm more comfortable with. was looking for a way to do it outside of a script.

This post was edited by carteblanche on Dec 12 2014 07:09pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Dec 12 2014 07:17pm
Quote (carteblanche @ Dec 12 2014 08:06pm)
never used $i groups before. that's what i needed  :thumbsup:

is that standard across most apps?

what is that, ruby? i'd prolly stick with a language i'm more comfortable with. was looking for a way to do it outside of a script.


so if you wrap your regex pattern in parenthesis it becomes what's known as a capture group, and the regex engine will assign it an identifier $i based on the order in which they are created. If you want something matched, but not captured. You can specify non capture groups with (?: ) I believe.

Also, to contrast the differences between mine and Abduct's regex pattern. He uses \b, which is the character class denoting word boundaries. I used \W* which is the character class any non-word and the * means 0 or more (greedy). In this case, they do the same thing really. \b might be more appropriate though, since by checking word boundaries you can leave out the left most character from the match, and thus not need that first capture group.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2014 08:06pm
i'm familiar with his \b pattern, just not the capture groups.

on another note, i just got hit in the face by a revelation. i kept picturing daffy duck with a 6 pack abs, and i just realized his name is more in line with firaga kidnapping little girls. i'll never look at abduct the same again.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll