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]> http://nyulmc.org/micros http://microscopynotes.com/ Voice direct only, no text or messages: 1-914-309-3270 and 1-646-501-0567 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
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 |
In reply to this post by Cammer, Michael-3
You have to escape the ( because it is a regex control character,
similar to .^$[] and the other s that I forgot... And because \ is a special character in strings, you also have toi escape that ;): s.replace("\\(", ""); Cheers, Stephan inOn Tue, 2020-08-11 at 21:05 +0000, Cammer, Michael wrote: > 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.com/v3/__http://nyulmc.org/micros__;!!Eh6p8Q!RFNNq9J6J-pz52s7uzVcuuuP9F5vP-5jd0valfeIRi4JDmwKC29Nx9RFUN_BzFjFQITP$ > > https://urldefense.com/v3/__http://microscopynotes.com/__;!!Eh6p8Q!RFNNq9J6J-pz52s7uzVcuuuP9F5vP-5jd0valfeIRi4JDmwKC29Nx9RFUN_BzDBQR0Dq$ > > > Voice direct only, no text or messages: 1-914-309-3270 and 1-646- > 501-0567 > > > -- > ImageJ mailing list: > https://urldefense.com/v3/__http://imagej.nih.gov/ij/list.html__;!!Eh6p8Q!RFNNq9J6J-pz52s7uzVcuuuP9F5vP-5jd0valfeIRi4JDmwKC29Nx9RFUN_BzOJg8dhR$ > ImageJ mailing list: http://imagej.nih.gov/ij/list.html signature.asc (499 bytes) Download Attachment |
In reply to this post by Cammer, Michael-3
Not an expert on the macro language, but a look at Java's String.replaceAll (which looks like the likely analog), the first argument is a regular expresssion. "(" and "()" are interpreted as regular expressions. The first is not syntactically correct, and the second is not what you intend.
-- Kenneth Sloan [hidden email] Vision is the art of seeing what is invisible to others. -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Cammer, Michael-3
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.html or 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 |
In reply to this post by Cammer, Michael-3
> On Aug 11, 2020, at 5:05 PM, Cammer, Michael <[hidden email]> wrote:
> > 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. This bug is fixed in the ImageJ 1.53c63 daily build. -wayne > > > 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]> http://nyulmc.org/micros http://microscopynotes.com/ > > Voice direct only, no text or messages: 1-914-309-3270 and 1-646-501-0567 > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Is this a bug? It seems to agree with the documentation.
-- Kenneth Sloan [hidden email] Vision is the art of seeing what is invisible to others. -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
> On Aug 12, 2020, at 4:45 PM, Kenneth Sloan <[hidden email]> wrote:
> > Is this a bug? It seems to agree with the documentation. I updated the documentation at http://wsr.imagej.net/developer/macro/functions.html so that it describes the behavior of replace(string,old,new) in the 1.53d64 daily build. Returns a string that results from replacing all occurrences of ‘old' in ‘string' with ‘new', where ‘old' is a single character string. If ‘old' is longer than one character, each substring of ‘string' that matches the regular expression ‘old' is replaced with ‘new'. When doing a simple string replacement, and ‘old' contains regular expression metacharacters ('.', '[', ']', '^', '$', etc.), you must escape them with a "\\". For example, to replace "[xx]" with "yy", use string=replace(string,"\\[xx\\]","yy"). Can be replaced with string.replace(old,new) in ImageJ 1.52t or later. -wayne -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |