Help with search and replace code
-
Hi,
I’m modding some game files and hoping to do this in bulk as there are hundreds to do! The format of the files is as follows:
name: "Medium 6x4" price: 26200 unlock: 0
How would I set this to change the ‘price’ (which could be any number from 1 to infinity) to 1, and the ‘unlock’ (which could be anything from 0 to infinity) to 0?
Thanks
-
If you set the find Search mode to Regular expression, you can search for variable numbers with
\d+
; example:price: \d+
-
Hello, @jessica-bruno, @alan-kilborn and All,
To complete the @alan-kilborn’s answer, I would say
SEARCH
(?-i)(?=price: )\d+
REPLACE
1
and :
SEARCH
(?-i)(?=unlock: )\d+
REPLACE
0
And, of course, tick the
Regular expression
search modeNote :
(?-i)
, at beginning of the regex, means that this regex is sensitive to case ( so NON-insensitive )Best Regards,
guy038
-
I think the virtue of my answer is that the OP could possibly remember it as a technique, whereas your answer likely results in “mind blown”, won’t remember.
-
@guy038 Don’t you mean
(?-i)(?<=price: )\d+
? -
Hi, @jessica-bruno, @paul-wormer, @alan-kilborn and All,
Oh… yes ! I was wrong and, in addition to Alan remark, my first post gave very poor information !
So, the exact search regexes are, of course :
- SEARCH
(?-i)(?<=price: )\d+
and
- SEARCH
(?-i)(?<=unlock: )\d+
BR
guy038
- SEARCH
-
@guy038 said in Help with search and replace code:
(?-i)(?<=price: )\d+
Thank you very much, this worked perfectly.
@Alan-Kilborn said in Help with search and replace code:
I think the virtue of my answer is that the OP could possibly remember it as a technique, whereas your answer likely results in “mind blown”, won’t remember.
I thank you for your belief that I could be even begin to understand any of the code involved with this! No chance!!
-
@Jessica-Bruno said in Help with search and replace code:
I thank you for your belief that I could be even begin to understand any of the code involved with this! No chance!!
😁
Maybe if I’d have pointed out that in
\d+
thed
stands for “digit”?The
\
before it makes thed
“special” (otherwise how would the program know you wanted something other than a reald
?).The
+
after it means “one or more”.So put it all together and
\d+
means “one or more digits”.See, not that hard, and possibly memorable?
-
Hello, @jessica-bruno, @paul-wormer, @alan-kilborn and All,
As @alan-kilborn, here are some more explanations on the regexes :
-
They both search for, at least, one character, considered as a digit, but ONLY IF preceded by the string
price:
or the stringunlock:
, with this exact case, and followed by aspace
character -
The four syntaxes
(?=aaaaa)
and(?!bbbbb)
, as well as(?<=ccccc)
and(?<!ddddd)
, are called look-arounds which allow you to search for any expression :-
ONLY IF followed by the
aaaaa
string -
ONLY IF NOT followed by the
bbbbb
string -
ONLY IF preceded by the
ccccc
string -
ONLY IF NOT preceded by the
ddddd
string
-
-
Note that the strings
aaaaa
,bbbbb
,ccccc
andddddd
are NEVER part of the expression to search for, but must be true in order to produce a valid match of the whole regex expression !
BR
guy038
-
-
@guy038 The not versions always seem deadly. For example
(?!bbbbb)
also matches between each of the letters withinbbbbb
and(?<!ddddd)
also matches between the letters withinddddd
. -
Hello, @jessica-bruno, @mkupper and All,
@mkupper, you’re right but these negative look-arounds are not intended to be used on their own but rather with a part outside the look-around(s !
Refer to the two tables below.
-
Copy the text of the first column in a new tab
-
Use the
Mark
dialog to get the text matched by each of these9
regexes, in a red/orange color ( tick thePurge for each search
option ) !
The results should be as below :
•---------------------•------------------------•----------------------•----------------------•-----------------------•-----------------------• | | 12345 | 12345(?=aaaaa) | 12345(?!bbbbb) | (?<=ccccc)12345 | (?<!ddddd)12345 | •---------------------•------------------------•----------------------•----------------------•-----------------------•-----------------------• | 12345aaaaa | X | x | X | | X | | 12345bbbbb | X | | | | X | | 12345zzzzz | X | | X | | X | | ccccc12345 | X | | X | X | X | | ddddd12345 | X | | X | | | | zzzzz12345 | X | | X | | X | | ccccc12345aaaaa | X | X | X | X | X | | ccccc12345bbbbb | X | | | X | X | | ccccc12345kkkkk | X | | X | X | X | | ddddd12345aaaaa | X | X | X | | | | ddddd12345bbbbb | X | | | | | | ddddd12345zzzzz | X | | X | | | | zzzzz12345aaaaa | X | X | X | | X | | zzzzz12345bbbbb | X | | | | X | | zzzzz12345zzzzz | X | | X | | X | •---------------------•------------------------•----------------------•----------------------•-----------------------•-----------------------•
•---------------------•------------------------------•--------------------------------•--------------------------------•--------------------------------• | | (?<=ccccc)12345(?=aaaaa) | (?<=ccccc)12345(?!bbbbb) | (?<!ddddd)12345(?=aaaaa) | (?<!ddddd)12345(?!bbbbb) | •---------------------•------------------------------•--------------------------------•--------------------------------•--------------------------------• | 12345aaaaa | | | X | X | | 12345bbbbb | | | | | | 12345zzzzz | | | | X | | ccccc12345 | | X | | X | | ddddd12345 | | | | | | zzzzz12345 | | | | X | | ccccc12345aaaaa | X | X | X | X | | ccccc12345bbbbb | | | | | | ccccc12345kkkkk | | X | | X | | ddddd12345aaaaa | | | | | | ddddd12345bbbbb | | | | | | ddddd12345zzzzz | | | | | | zzzzz12345aaaaa | | | X | X | | zzzzz12345bbbbb | | | | | | zzzzz12345zzzzz | | | | X | •---------------------•------------------------------•--------------------------------•--------------------------------•--------------------------------•
Best Regards,
guy038
-