Skip to main content
Tweeted twitter.com/StackMma/status/1217371012054110208
Became Hot Network Question
added 119 characters in body
Source Link
abcd
  • 483
  • 2
  • 8

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns.

The problem is to translate a string into Morse code, for which I assume here for simplicity is defined by rules contained in the list "translationPatterns".

I cannot understand why the following three cases give such results (in the example I show, The letters "e" and "t" translate into "." and "-" respectively, to keep it simple):

StringReplace["eeeeee ttt ee", 
 Join[translationPatterns, {___ ~~ "ttt" ~~ ___ :> "abc"} ]]

"......abc" (I cannot understand why what's on the left of "ttt" is not replaced as well...)

StringReplace["eeeeee ttt ee", 
 Join[translationPatterns, {__ ~~ "ttt" ~~ _ :> "abc"} ]]

"......abc.." (This is equally strange, though coherent with the previous result.)

StringReplace["eeeeee ttt ee", 
 Join[{___ ~~ "ttt" ~~ ___ :> "abc"}, translationPatterns]

"abc" (This is what I would expect in the first case.)

Why the first case does not provide the same result as the last one?

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns.

The problem is to translate a string into Morse code, for which I assume here for simplicity is defined by rules contained in the list "translationPatterns".

I cannot understand why the following three cases give such results (in the example I show, The letters "e" and "t" translate into "." and "-" respectively, to keep it simple):

StringReplace["eeeeee ttt ee", 
 Join[translationPatterns, {___ ~~ "ttt" ~~ ___ :> "abc"} ]]

"......abc" (I cannot understand why what's on the left of "ttt" is not replaced as well...)

StringReplace["eeeeee ttt ee", 
 Join[translationPatterns, {__ ~~ "ttt" ~~ _ :> "abc"} ]]

"......abc.."

StringReplace["eeeeee ttt ee", 
 Join[{___ ~~ "ttt" ~~ ___ :> "abc"}, translationPatterns]

"abc"

Why the first case does not provide the same result as the last one?

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns.

The problem is to translate a string into Morse code, for which I assume here for simplicity is defined by rules contained in the list "translationPatterns".

I cannot understand why the following three cases give such results (in the example I show, The letters "e" and "t" translate into "." and "-" respectively, to keep it simple):

StringReplace["eeeeee ttt ee", 
 Join[translationPatterns, {___ ~~ "ttt" ~~ ___ :> "abc"} ]]

"......abc" (I cannot understand why what's on the left of "ttt" is not replaced as well...)

StringReplace["eeeeee ttt ee", 
 Join[translationPatterns, {__ ~~ "ttt" ~~ _ :> "abc"} ]]

"......abc.." (This is equally strange, though coherent with the previous result.)

StringReplace["eeeeee ttt ee", 
 Join[{___ ~~ "ttt" ~~ ___ :> "abc"}, translationPatterns]

"abc" (This is what I would expect in the first case.)

Why the first case does not provide the same result as the last one?

deleted 4 characters in body
Source Link
abcd
  • 483
  • 2
  • 8

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns.

The problem is to translate a string into Morse code, for which I assume here for simplicity is defined by rules contained in the list "translation_patterns""translationPatterns".

I cannot understand why the following three cases give such results (in the example I show, The letters "e" and "t" translate into "." and "-" respectively, to keep it simple):

StringReplace["eeeeee ttt ee", 
 Join[translation_patternsJoin[translationPatterns, {___ ~~ "ttt" ~~ ___ :> "abc"} ]]

"......abc" (I cannot understand why what's on the left of "ttt" is not replaced as well...)

StringReplace["eeeeee ttt ee", 
 Join[translation_patternsJoin[translationPatterns, {__ ~~ "ttt" ~~ _ :> "abc"} ]]

"......abc.."

StringReplace["eeeeee ttt ee", 
 Join[{___ ~~ "ttt" ~~ ___ :> "abc"}, translation_patterns]translationPatterns]

"abc"

Why the first case does not provide the same result as the last one?

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns.

The problem is to translate a string into Morse code, for which I assume here for simplicity is defined by rules contained in the list "translation_patterns".

I cannot understand why the following three cases give such results (in the example I show, The letters "e" and "t" translate into "." and "-" respectively, to keep it simple):

StringReplace["eeeeee ttt ee", 
 Join[translation_patterns, {___ ~~ "ttt" ~~ ___ :> "abc"} ]]

"......abc" (I cannot understand why what's on the left of "ttt" is not replaced as well...)

StringReplace["eeeeee ttt ee", 
 Join[translation_patterns, {__ ~~ "ttt" ~~ _ :> "abc"} ]]

"......abc.."

StringReplace["eeeeee ttt ee", 
 Join[{___ ~~ "ttt" ~~ ___ :> "abc"}, translation_patterns]

"abc"

Why the first case does not provide the same result as the last one?

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns.

The problem is to translate a string into Morse code, for which I assume here for simplicity is defined by rules contained in the list "translationPatterns".

I cannot understand why the following three cases give such results (in the example I show, The letters "e" and "t" translate into "." and "-" respectively, to keep it simple):

StringReplace["eeeeee ttt ee", 
 Join[translationPatterns, {___ ~~ "ttt" ~~ ___ :> "abc"} ]]

"......abc" (I cannot understand why what's on the left of "ttt" is not replaced as well...)

StringReplace["eeeeee ttt ee", 
 Join[translationPatterns, {__ ~~ "ttt" ~~ _ :> "abc"} ]]

"......abc.."

StringReplace["eeeeee ttt ee", 
 Join[{___ ~~ "ttt" ~~ ___ :> "abc"}, translationPatterns]

"abc"

Why the first case does not provide the same result as the last one?

Source Link
abcd
  • 483
  • 2
  • 8

StringReplace with multiple patterns

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns.

The problem is to translate a string into Morse code, for which I assume here for simplicity is defined by rules contained in the list "translation_patterns".

I cannot understand why the following three cases give such results (in the example I show, The letters "e" and "t" translate into "." and "-" respectively, to keep it simple):

StringReplace["eeeeee ttt ee", 
 Join[translation_patterns, {___ ~~ "ttt" ~~ ___ :> "abc"} ]]

"......abc" (I cannot understand why what's on the left of "ttt" is not replaced as well...)

StringReplace["eeeeee ttt ee", 
 Join[translation_patterns, {__ ~~ "ttt" ~~ _ :> "abc"} ]]

"......abc.."

StringReplace["eeeeee ttt ee", 
 Join[{___ ~~ "ttt" ~~ ___ :> "abc"}, translation_patterns]

"abc"

Why the first case does not provide the same result as the last one?