Posted by
Fred Damen on
Aug 12, 2020; 1:44am
URL: http://imagej.273.s1.nabble.com/s-replace-question-tp5023836p5023840.html
Greetings Michael,
Two things...
First, as eluded to by another reply, the strings that are passed to
replace are parsed twice, first by the Java compiler (in this case
interpreting the escape character sequence, i.e., \...), and second by the
replace method as regular expressions (specifically here the
group(ing)/extraction sequence, i.e., "(...)"). In the last example you
gave the actual matching regular expression that you specified is the
nothingness in between the (), i.e., an empty string, and it matched the
nothingness in between each character in s. Useful if you want to break s
up into individual characters, otherwise annoying.
Second, I suspect that you are trying to change
"c:3/3 - KO_1 nM_rep2_007.nd2 (series 1)"
into
"KO_1 nM_rep2_007",
where
s.replace("^.*- (.*)\\.nd2.*$","\\1");
should work.
Java compiler will convert this to
s.replace("^.*- (.*)\.nd2.*$","\1");
replace will look for string that begins with anything ("." is any single
character, and "*" is zero or more of the previous thing) followed by "-
", followed by anything that is not a "." and remember this as \1,
followed by anything up to the end of the string, THEN set the entire
string to what it remembered as \1.
see
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.htmlor google perl regex tutorials.
Enjoy,
Fred
On Tue, August 11, 2020 4:08 pm, Cammer, Michael wrote:
> Sorry I left out that before trying to remove the parentheses, there is
> the following code:
>
>
> i = 27;
> newTitle = getMetadata("Label");
> print("\nOriginal: " + newTitle);
> newTitle = newTitle.replace("c:3/3 - ", "");
> newTitle = newTitle.replace(".nd2", "");
> newTitle = newTitle.replace("series 1", "");
>
>
> ________________________________
> From: Cammer, Michael <
[hidden email]>
> Sent: Tuesday, August 11, 2020 5:05:56 PM
> To:
[hidden email]
> Subject: s.replace question
>
> [EXTERNAL]
>
> I have been trying to remove parentheses from a string using the s.replace
> command and have been getting strange results. (Practically, I was able
> to solve the problem by cutting the string apart, trimming out the
> parentheses and reassembling the string, but am asking specifically about
> the s.replace behavior.)
>
>
> Code such as this
>
> newTitle = newTitle.replace("(", "");
>
> returns an error message.
>
>
> Therefore, I tried setting a variable to "()" and used the following
> syntax:
>
> print("Before trying to replace parentheses: " + newTitle);
> parentheses = fromCharCode(40, 41);
> print(parentheses);
> newTitle = newTitle.replace(parentheses, "");
> newTitle = newTitle + IJ.pad(i, 3);
> print("Processed: " + newTitle);
> with output that does not remove the parentheses:
>
> Original: c:3/3 - KO_1 nM_rep2_007.nd2 (series 1)
> Before trying to replace parentheses: KO_1 nM_rep2_007 ()
> ()
> Processed: KO_1 nM_rep2_007 ()027
>
> And odder, the output in this case inserts "xx" between every character:
>
>
> print("Before trying to replace parentheses: " + newTitle);
> parentheses = fromCharCode(40, 41);
> print(parentheses);
> newTitle = newTitle.replace(parentheses, "xx");
> newTitle = newTitle + IJ.pad(i, 3);
> print("Processed: " + newTitle);
>
>
>
> Original: c:3/3 - KO_1 nM_rep2_007.nd2 (series 1)
> Before trying to replace parentheses: KO_1 nM_rep2_007 ()
> ()
> Processed: xxKxxOxx_xx1xx xxnxxMxx_xxrxxexxpxx2xx_xx0xx0xx7xx xx(xx)xx027
>
>
>
> Any ideas?
>
>
> Thank you!
>
>
> Michael Cammer, Sr Research Scientist, DART Microscopy Laboratory
>
> NYU Langone Health, 540 First Avenue, SK2 Microscopy Suite, New York, NY
> 10016
>
>
[hidden email]<mailto:
[hidden email]>
>
https://urldefense.proofpoint.com/v2/url?u=http-3A__nyulmc.org_micros&d=DwIFAw&c=j5oPpO0eBH1iio48DtsedeElZfc04rx3ExJHeIIZuCs&r=E0xNnPAQpUbDiPlC50tp7rW2nBkvV7fujQf0RknE5bU&m=2rgsSf1b19Xe2HaA9-8saPs1zuKolzRHaBlzt84sxtY&s=WL9zhPlv6pElXz3GCVJj0nYhuaIxKWC-b9vbI0iMrZc&e=>
>
https://urldefense.proofpoint.com/v2/url?u=http-3A__microscopynotes.com_&d=DwIFAw&c=j5oPpO0eBH1iio48DtsedeElZfc04rx3ExJHeIIZuCs&r=E0xNnPAQpUbDiPlC50tp7rW2nBkvV7fujQf0RknE5bU&m=2rgsSf1b19Xe2HaA9-8saPs1zuKolzRHaBlzt84sxtY&s=wOP7EA9iAuKgLu-oT_P201QJl0TAOG_eXP9nFQhqmjM&e=>
> Voice direct only, no text or messages: 1-914-309-3270 and 1-646-501-0567
>
>
> --
> ImageJ mailing list:
>
https://urldefense.proofpoint.com/v2/url?u=http-3A__imagej.nih.gov_ij_list.html&d=DwIFAw&c=j5oPpO0eBH1iio48DtsedeElZfc04rx3ExJHeIIZuCs&r=E0xNnPAQpUbDiPlC50tp7rW2nBkvV7fujQf0RknE5bU&m=2rgsSf1b19Xe2HaA9-8saPs1zuKolzRHaBlzt84sxtY&s=38Mrki7TDUGWBUKrPoDsDw7lCrX1D8kH0lxy8bqCezk&e=>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html>
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html