Replace "/" and "\" with "_"

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Replace "/" and "\" with "_"

mmettlen
I've searched, tried and failed: My macro extracts the directory of an image.
From this string, I then successfully extract a sub-string that still
contains  "/" and "\". How can I replace those with "_"?
E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_





--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Replace "/" and "\" with "_"

Andrew-2
*replace(string, old, new)*
Returns the new string that results from replacing all occurrences of *old*
 in *string* with *new*, where *old* and *new* are single character
strings. If *old* or *new* are longer than one character, each substring of
*string* that matches the regular expression
<http://en.wikipedia.org/wiki/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")*. See also:matches
<https://imagej.nih.gov/ij/developer/macro/functions.html#matches>.

So it would be:

var = replace(var, "/", "_");
var = replace(var, "\","_");


Andrew

On Fri, Sep 21, 2018 at 7:42 AM mmettlen <[hidden email]>
wrote:

> I've searched, tried and failed: My macro extracts the directory of an
> image.
> From this string, I then successfully extract a sub-string that still
> contains  "/" and "\". How can I replace those with "_"?
> E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_
>
>
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Replace "/" and "\" with "_"

Andrew-2
Apologies, forgot the escape character, so "\\" and "\/" instead

On Fri, Sep 21, 2018 at 11:55 AM Andrew <[hidden email]> wrote:

> *replace(string, old, new)*
> Returns the new string that results from replacing all occurrences of
> *old* in *string* with *new*, where *old* and *new* are single character
> strings. If *old* or *new* are longer than one character, each substring
> of *string* that matches the regular expression
> <http://en.wikipedia.org/wiki/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")*. See also:matches
> <https://imagej.nih.gov/ij/developer/macro/functions.html#matches>.
>
> So it would be:
>
> var = replace(var, "/", "_");
> var = replace(var, "\","_");
>
>
> Andrew
>
> On Fri, Sep 21, 2018 at 7:42 AM mmettlen <
> [hidden email]> wrote:
>
>> I've searched, tried and failed: My macro extracts the directory of an
>> image.
>> From this string, I then successfully extract a sub-string that still
>> contains  "/" and "\". How can I replace those with "_"?
>> E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_
>>
>>
>>
>>
>>
>> --
>> Sent from: http://imagej.1557.x6.nabble.com/
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Replace "/" and "\" with "_"

Fred Damen
greetings,

So, the way you wrote the escape character in the last line of code confused
me, so I created a few lines of macro code to test this out:
s="\";
print("s="+s);
s=replace(s,"\","_");
print("s="+s);

The first two lines produce:
s=";

Which seems to be in err, as this should be a compile time error; and where
did the ';' come from?

The last two lines produced a compile time popup error message complaining
about a undefined variable.

Bonus question:
If "." is not a regex then how is one supposed to replace every character with
another?

Fred

PS: \ in \/ is a noop.

On Fri, September 21, 2018 10:56 am, Andrew wrote:

> Apologies, forgot the escape character, so "\\" and "\/" instead
>
> On Fri, Sep 21, 2018 at 11:55 AM Andrew <[hidden email]> wrote:
>
>> *replace(string, old, new)*
>> Returns the new string that results from replacing all occurrences of
>> *old* in *string* with *new*, where *old* and *new* are single character
>> strings. If *old* or *new* are longer than one character, each substring
>> of *string* that matches the regular expression
>> <http://en.wikipedia.org/wiki/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")*. See also:matches
>> <https://imagej.nih.gov/ij/developer/macro/functions.html#matches>.
>>
>> So it would be:
>>
>> var = replace(var, "/", "_");
>> var = replace(var, "\","_");
>>
>>
>> Andrew
>>
>> On Fri, Sep 21, 2018 at 7:42 AM mmettlen <
>> [hidden email]> wrote:
>>
>>> I've searched, tried and failed: My macro extracts the directory of an
>>> image.
>>> From this string, I then successfully extract a sub-string that still
>>> contains  "/" and "\". How can I replace those with "_"?
>>> E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Sent from: http://imagej.1557.x6.nabble.com/
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>>
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Replace "/" and "\" with "_"

Herbie
In reply to this post by Andrew-2
Good day Andrew,

did you really try it?

It works for me for the escaped forward slash but not for the escaped
backwards slash.

Regards

Herbie


::::::::::::::::::::::::::::::::::::
Am 21.09.18 um 17:56 schrieb Andrew:

> Apologies, forgot the escape character, so "\\" and "\/" instead
>
> On Fri, Sep 21, 2018 at 11:55 AM Andrew <[hidden email]> wrote:
>
>> *replace(string, old, new)*
>> Returns the new string that results from replacing all occurrences of
>> *old* in *string* with *new*, where *old* and *new* are single character
>> strings. If *old* or *new* are longer than one character, each substring
>> of *string* that matches the regular expression
>> <http://en.wikipedia.org/wiki/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")*. See also:matches
>> <https://imagej.nih.gov/ij/developer/macro/functions.html#matches>.
>>
>> So it would be:
>>
>> var = replace(var, "/", "_");
>> var = replace(var, "\","_");
>>
>>
>> Andrew
>>
>> On Fri, Sep 21, 2018 at 7:42 AM mmettlen <
>> [hidden email]> wrote:
>>
>>> I've searched, tried and failed: My macro extracts the directory of an
>>> image.
>>>  From this string, I then successfully extract a sub-string that still
>>> contains  "/" and "\". How can I replace those with "_"?
>>> E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Sent from: http://imagej.1557.x6.nabble.com/
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>>
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Replace "/" and "\" with "_"

Michael Schmid-3
Hi everyone,

the backslash is a very special character, which makes it a bit tricky:

When you type a backslash in a String literal, it will escape the next
character, so you need to type two backslashes to get one.
In "replace", you need a regular expression. In regular expressions, the
backslash also has a special meaning for escaping, so you need two
backslashes in the "search" string. This means you have to type four
backslashes.


Example:

v="\\Folder1/Folder2/";
v1=replace(v, "/", "_");
v2=replace(v1, "\\\\", "_");
print(v);
print(v2);


Michael
________________________________________________________________
On 21/09/2018 18:42, Herbie wrote:
 > Good day Andrew,
 >
 > did you really try it?
 >
 > It works for me for the escaped forward slash but not for the escaped
backwards slash.
 >
 > Regards
 >
 > Herbie
 >
 >
 > ::::::::::::::::::::::::::::::::::::
 > Am 21.09.18 um 17:56 schrieb Andrew:
 >> Apologies, forgot the escape character, so "\\" and "\/" instead
 >>
 >> On Fri, Sep 21, 2018 at 11:55 AM Andrew <[hidden email]> wrote:
 >>
 >>> *replace(string, old, new)*
 >>> Returns the new string that results from replacing all occurrences of
 >>> *old* in *string* with *new*, where *old* and *new* are single
character
 >>> strings. If *old* or *new* are longer than one character, each
substring
 >>> of *string* that matches the regular expression
 >>> <http://en.wikipedia.org/wiki/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")*. See also:matches
 >>> <https://imagej.nih.gov/ij/developer/macro/functions.html#matches>.
 >>>
 >>> So it would be:
 >>>
 >>> var = replace(var, "/", "_");
 >>> var = replace(var, "\","_");
 >>>
 >>>
 >>> Andrew
 >>>
 >>> On Fri, Sep 21, 2018 at 7:42 AM mmettlen <
 >>> [hidden email]> wrote:
 >>>
 >>>> I've searched, tried and failed: My macro extracts the directory of an
 >>>> image.
 >>>>  From this string, I then successfully extract a sub-string that still
 >>>> contains  "/" and "\". How can I replace those with "_"?
 >>>> E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_
 >>>>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Replace "/" and "\" with "_"

Andrew-2
Not trying to confuse everyone, but I just tested it and

v2=replace(v1, "\\\\", "_");

was producing the same results for me as:

v2=replace(v1, "\\", "_");

which seems a little odd, but also means you can't replace two adjacent
backslashes in a string with a single character using this command.


Andrew


On Fri, Sep 21, 2018 at 1:36 PM Michael Schmid <[hidden email]>
wrote:

> Hi everyone,
>
> the backslash is a very special character, which makes it a bit tricky:
>
> When you type a backslash in a String literal, it will escape the next
> character, so you need to type two backslashes to get one.
> In "replace", you need a regular expression. In regular expressions, the
> backslash also has a special meaning for escaping, so you need two
> backslashes in the "search" string. This means you have to type four
> backslashes.
>
>
> Example:
>
> v="\\Folder1/Folder2/";
> v1=replace(v, "/", "_");
> v2=replace(v1, "\\\\", "_");
> print(v);
> print(v2);
>
>
> Michael
> ________________________________________________________________
> On 21/09/2018 18:42, Herbie wrote:
>  > Good day Andrew,
>  >
>  > did you really try it?
>  >
>  > It works for me for the escaped forward slash but not for the escaped
> backwards slash.
>  >
>  > Regards
>  >
>  > Herbie
>  >
>  >
>  > ::::::::::::::::::::::::::::::::::::
>  > Am 21.09.18 um 17:56 schrieb Andrew:
>  >> Apologies, forgot the escape character, so "\\" and "\/" instead
>  >>
>  >> On Fri, Sep 21, 2018 at 11:55 AM Andrew <[hidden email]> wrote:
>  >>
>  >>> *replace(string, old, new)*
>  >>> Returns the new string that results from replacing all occurrences of
>  >>> *old* in *string* with *new*, where *old* and *new* are single
> character
>  >>> strings. If *old* or *new* are longer than one character, each
> substring
>  >>> of *string* that matches the regular expression
>  >>> <http://en.wikipedia.org/wiki/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")*. See also:matches
>  >>> <https://imagej.nih.gov/ij/developer/macro/functions.html#matches>.
>  >>>
>  >>> So it would be:
>  >>>
>  >>> var = replace(var, "/", "_");
>  >>> var = replace(var, "\","_");
>  >>>
>  >>>
>  >>> Andrew
>  >>>
>  >>> On Fri, Sep 21, 2018 at 7:42 AM mmettlen <
>  >>> [hidden email]> wrote:
>  >>>
>  >>>> I've searched, tried and failed: My macro extracts the directory of
> an
>  >>>> image.
>  >>>>  From this string, I then successfully extract a sub-string that
> still
>  >>>> contains  "/" and "\". How can I replace those with "_"?
>  >>>> E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_
>  >>>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Replace "/" and "\" with "_"

Andrew-2
Not trying to flood the listserv, but just tested and

v2=replace(v1, "\\\\\\\\", "_");

can be used to replace two adjacent backslashes with a single character, so
I was wrong.

On Fri, Sep 21, 2018 at 1:46 PM Andrew <[hidden email]> wrote:

> Not trying to confuse everyone, but I just tested it and
>
> v2=replace(v1, "\\\\", "_");
>
> was producing the same results for me as:
>
> v2=replace(v1, "\\", "_");
>
> which seems a little odd, but also means you can't replace two adjacent
> backslashes in a string with a single character using this command.
>
>
> Andrew
>
>
> On Fri, Sep 21, 2018 at 1:36 PM Michael Schmid <[hidden email]>
> wrote:
>
>> Hi everyone,
>>
>> the backslash is a very special character, which makes it a bit tricky:
>>
>> When you type a backslash in a String literal, it will escape the next
>> character, so you need to type two backslashes to get one.
>> In "replace", you need a regular expression. In regular expressions, the
>> backslash also has a special meaning for escaping, so you need two
>> backslashes in the "search" string. This means you have to type four
>> backslashes.
>>
>>
>> Example:
>>
>> v="\\Folder1/Folder2/";
>> v1=replace(v, "/", "_");
>> v2=replace(v1, "\\\\", "_");
>> print(v);
>> print(v2);
>>
>>
>> Michael
>> ________________________________________________________________
>> On 21/09/2018 18:42, Herbie wrote:
>>  > Good day Andrew,
>>  >
>>  > did you really try it?
>>  >
>>  > It works for me for the escaped forward slash but not for the escaped
>> backwards slash.
>>  >
>>  > Regards
>>  >
>>  > Herbie
>>  >
>>  >
>>  > ::::::::::::::::::::::::::::::::::::
>>  > Am 21.09.18 um 17:56 schrieb Andrew:
>>  >> Apologies, forgot the escape character, so "\\" and "\/" instead
>>  >>
>>  >> On Fri, Sep 21, 2018 at 11:55 AM Andrew <[hidden email]> wrote:
>>  >>
>>  >>> *replace(string, old, new)*
>>  >>> Returns the new string that results from replacing all occurrences of
>>  >>> *old* in *string* with *new*, where *old* and *new* are single
>> character
>>  >>> strings. If *old* or *new* are longer than one character, each
>> substring
>>  >>> of *string* that matches the regular expression
>>  >>> <http://en.wikipedia.org/wiki/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")*. See also:matches
>>  >>> <https://imagej.nih.gov/ij/developer/macro/functions.html#matches>.
>>  >>>
>>  >>> So it would be:
>>  >>>
>>  >>> var = replace(var, "/", "_");
>>  >>> var = replace(var, "\","_");
>>  >>>
>>  >>>
>>  >>> Andrew
>>  >>>
>>  >>> On Fri, Sep 21, 2018 at 7:42 AM mmettlen <
>>  >>> [hidden email]> wrote:
>>  >>>
>>  >>>> I've searched, tried and failed: My macro extracts the directory of
>> an
>>  >>>> image.
>>  >>>>  From this string, I then successfully extract a sub-string that
>> still
>>  >>>> contains  "/" and "\". How can I replace those with "_"?
>>  >>>> E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_
>>  >>>>
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Replace "/" and "\" with "_"

Michael Schmid-3
In reply to this post by Andrew-2
Hi Andrew,

yes, in this special case where you replace *one* character with *one*
other character, the ImageJ macro "replace" function also accepts a
character, not a regexp.
It won't work if you replace it with more than one character:

v="\\foo/bar";
v1=replace(v, "\\", "wasBackslash"); // error

Also, as soon as there is something behind the backslash and you want to
replace it, you definitively need four:

This works:
v="\\foo/bar";
v1=replace(v, "\\\\foo", "wasBackslashFoo");
print(v);
print(v1);

The following does no replacement, because it would match a formfeed
(\f) character followed by two 'o' characters:
v="\\foo/bar";
v1=replace(v, "\\foo", "wasBackslashFoo");
print(v);
print(v1);

And not being aware of how many backslashes you need, you might get an
unexpected replacement here:
v="\\nobody\nlinetwo";
v1=replace(v, "\\n", "_dontThinkThisWasBackslash-n_");
print(v);
print(v1);


So, I consider it a safer solution to always do it the "regular
expression" way; then you won't wonder why it works in one case and not
in another.


Michael
________________________________________________________________
On 21/09/2018 19:46, Andrew wrote:

> Not trying to confuse everyone, but I just tested it and
>
> v2=replace(v1, "\\\\", "_");
>
> was producing the same results for me as:
>
> v2=replace(v1, "\\", "_");
>
> which seems a little odd, but also means you can't replace two adjacent
> backslashes in a string with a single character using this command.
>
>
> Andrew
>
>
> On Fri, Sep 21, 2018 at 1:36 PM Michael Schmid <[hidden email]>
> wrote:
>
>> Hi everyone,
>>
>> the backslash is a very special character, which makes it a bit tricky:
>>
>> When you type a backslash in a String literal, it will escape the next
>> character, so you need to type two backslashes to get one.
>> In "replace", you need a regular expression. In regular expressions, the
>> backslash also has a special meaning for escaping, so you need two
>> backslashes in the "search" string. This means you have to type four
>> backslashes.
>>
>>
>> Example:
>>
>> v="\\Folder1/Folder2/";
>> v1=replace(v, "/", "_");
>> v2=replace(v1, "\\\\", "_");
>> print(v);
>> print(v2);
>>
>>
>> Michael
>> ________________________________________________________________
>> On 21/09/2018 18:42, Herbie wrote:
>>   > Good day Andrew,
>>   >
>>   > did you really try it?
>>   >
>>   > It works for me for the escaped forward slash but not for the escaped
>> backwards slash.
>>   >
>>   > Regards
>>   >
>>   > Herbie
>>   >
>>   >
>>   > ::::::::::::::::::::::::::::::::::::
>>   > Am 21.09.18 um 17:56 schrieb Andrew:
>>   >> Apologies, forgot the escape character, so "\\" and "\/" instead
>>   >>
>>   >> On Fri, Sep 21, 2018 at 11:55 AM Andrew <[hidden email]> wrote:
>>   >>
>>   >>> *replace(string, old, new)*
>>   >>> Returns the new string that results from replacing all occurrences of
>>   >>> *old* in *string* with *new*, where *old* and *new* are single
>> character
>>   >>> strings. If *old* or *new* are longer than one character, each
>> substring
>>   >>> of *string* that matches the regular expression
>>   >>> <http://en.wikipedia.org/wiki/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")*. See also:matches
>>   >>> <https://imagej.nih.gov/ij/developer/macro/functions.html#matches>.
>>   >>>
>>   >>> So it would be:
>>   >>>
>>   >>> var = replace(var, "/", "_");
>>   >>> var = replace(var, "\","_");
>>   >>>
>>   >>>
>>   >>> Andrew
>>   >>>
>>   >>> On Fri, Sep 21, 2018 at 7:42 AM mmettlen <
>>   >>> [hidden email]> wrote:
>>   >>>
>>   >>>> I've searched, tried and failed: My macro extracts the directory of
>> an
>>   >>>> image.
>>   >>>>  From this string, I then successfully extract a sub-string that
>> still
>>   >>>> contains  "/" and "\". How can I replace those with "_"?
>>   >>>> E.g.: \Folder1/Folder2/ should become _Folder1_Folder2_

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html