Replacing Strings at specific locations
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
text = "Donc", "j'obtiens", "<i>aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â -
[Beta]âÂÂ) + ... + aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.</i>",
"<i>aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + ... +
aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.</i>", "Donc en particulier",
"ça veut dire que si je multiplie par <i>c</i>", "pour tout nombre réel
<i>c</i>"
Now my question is:
How can I get the position of in the list of every object that starts with
<i>
?How can I replace the
<i>
at the start of that object and the</i>
at the end of that object by another string ?
By "object" I really mean a list object: for instance, I don't want the <i>
and </i>
to be replaced in "ça veut dire que si je multiplie par <i>c</i>
", since this object of the list does not start with an <i>
.
EDIT: To clear any confusion, here is how the text could look like afterwards:
"Donc", "j'obtiens", "REPLACE([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â -
[Beta]âÂÂ) + ... + aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"REPLACEaáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + ... +
aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.REPLACE", "Donc en particulier",
"ça veut dire que si je multiplie par <i>c</i>", "pour tout nombre réel
<i>c</i>"
string-manipulation
 |Â
show 2 more comments
up vote
1
down vote
favorite
text = "Donc", "j'obtiens", "<i>aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â -
[Beta]âÂÂ) + ... + aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.</i>",
"<i>aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + ... +
aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.</i>", "Donc en particulier",
"ça veut dire que si je multiplie par <i>c</i>", "pour tout nombre réel
<i>c</i>"
Now my question is:
How can I get the position of in the list of every object that starts with
<i>
?How can I replace the
<i>
at the start of that object and the</i>
at the end of that object by another string ?
By "object" I really mean a list object: for instance, I don't want the <i>
and </i>
to be replaced in "ça veut dire que si je multiplie par <i>c</i>
", since this object of the list does not start with an <i>
.
EDIT: To clear any confusion, here is how the text could look like afterwards:
"Donc", "j'obtiens", "REPLACE([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â -
[Beta]âÂÂ) + ... + aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"REPLACEaáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + ... +
aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.REPLACE", "Donc en particulier",
"ça veut dire que si je multiplie par <i>c</i>", "pour tout nombre réel
<i>c</i>"
string-manipulation
Could try something likePosition[text, s_String /; StringMatchQ[s, "<i>*</i>"]]
, but that won't catch the last two items with italics tags since they don't actually "start with<i>
", as the criterion you mentioned.
â J. M. is computer-lessâ¦
27 mins ago
@J.M.iscomputer-less That's actually perfect ! Maybe I was not clear enough, but I don't want to catch "<i>" where the sentence does not start with "<i>". Can you post it as an answer ? Then I can accept it, if you also know how to replace the "<i>" and "</i>" in the found sentences.
â james
23 mins ago
If just for replacement, it seems no need to get their positions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
22 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó Okay, interesting. How would you do it then ?
â james
22 mins ago
You did not mention what kind of replacement you wanted to do. Is it derived from the matching string, or something completely different?
â J. M. is computer-lessâ¦
20 mins ago
 |Â
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
text = "Donc", "j'obtiens", "<i>aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â -
[Beta]âÂÂ) + ... + aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.</i>",
"<i>aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + ... +
aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.</i>", "Donc en particulier",
"ça veut dire que si je multiplie par <i>c</i>", "pour tout nombre réel
<i>c</i>"
Now my question is:
How can I get the position of in the list of every object that starts with
<i>
?How can I replace the
<i>
at the start of that object and the</i>
at the end of that object by another string ?
By "object" I really mean a list object: for instance, I don't want the <i>
and </i>
to be replaced in "ça veut dire que si je multiplie par <i>c</i>
", since this object of the list does not start with an <i>
.
EDIT: To clear any confusion, here is how the text could look like afterwards:
"Donc", "j'obtiens", "REPLACE([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â -
[Beta]âÂÂ) + ... + aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"REPLACEaáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + ... +
aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.REPLACE", "Donc en particulier",
"ça veut dire que si je multiplie par <i>c</i>", "pour tout nombre réel
<i>c</i>"
string-manipulation
text = "Donc", "j'obtiens", "<i>aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â -
[Beta]âÂÂ) + ... + aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.</i>",
"<i>aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + ... +
aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.</i>", "Donc en particulier",
"ça veut dire que si je multiplie par <i>c</i>", "pour tout nombre réel
<i>c</i>"
Now my question is:
How can I get the position of in the list of every object that starts with
<i>
?How can I replace the
<i>
at the start of that object and the</i>
at the end of that object by another string ?
By "object" I really mean a list object: for instance, I don't want the <i>
and </i>
to be replaced in "ça veut dire que si je multiplie par <i>c</i>
", since this object of the list does not start with an <i>
.
EDIT: To clear any confusion, here is how the text could look like afterwards:
"Donc", "j'obtiens", "REPLACE([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â -
[Beta]âÂÂ) + ... + aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"REPLACEaáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + aáµ¢âÂÂ([Alpha]â - [Beta]âÂÂ) + ... +
aâÂÂâÂÂ([Alpha]â - [Beta]âÂÂ) = báµ¢ - báµ¢ = 0.REPLACE", "Donc en particulier",
"ça veut dire que si je multiplie par <i>c</i>", "pour tout nombre réel
<i>c</i>"
string-manipulation
string-manipulation
edited 16 mins ago
asked 34 mins ago
james
639418
639418
Could try something likePosition[text, s_String /; StringMatchQ[s, "<i>*</i>"]]
, but that won't catch the last two items with italics tags since they don't actually "start with<i>
", as the criterion you mentioned.
â J. M. is computer-lessâ¦
27 mins ago
@J.M.iscomputer-less That's actually perfect ! Maybe I was not clear enough, but I don't want to catch "<i>" where the sentence does not start with "<i>". Can you post it as an answer ? Then I can accept it, if you also know how to replace the "<i>" and "</i>" in the found sentences.
â james
23 mins ago
If just for replacement, it seems no need to get their positions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
22 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó Okay, interesting. How would you do it then ?
â james
22 mins ago
You did not mention what kind of replacement you wanted to do. Is it derived from the matching string, or something completely different?
â J. M. is computer-lessâ¦
20 mins ago
 |Â
show 2 more comments
Could try something likePosition[text, s_String /; StringMatchQ[s, "<i>*</i>"]]
, but that won't catch the last two items with italics tags since they don't actually "start with<i>
", as the criterion you mentioned.
â J. M. is computer-lessâ¦
27 mins ago
@J.M.iscomputer-less That's actually perfect ! Maybe I was not clear enough, but I don't want to catch "<i>" where the sentence does not start with "<i>". Can you post it as an answer ? Then I can accept it, if you also know how to replace the "<i>" and "</i>" in the found sentences.
â james
23 mins ago
If just for replacement, it seems no need to get their positions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
22 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó Okay, interesting. How would you do it then ?
â james
22 mins ago
You did not mention what kind of replacement you wanted to do. Is it derived from the matching string, or something completely different?
â J. M. is computer-lessâ¦
20 mins ago
Could try something like
Position[text, s_String /; StringMatchQ[s, "<i>*</i>"]]
, but that won't catch the last two items with italics tags since they don't actually "start with <i>
", as the criterion you mentioned.â J. M. is computer-lessâ¦
27 mins ago
Could try something like
Position[text, s_String /; StringMatchQ[s, "<i>*</i>"]]
, but that won't catch the last two items with italics tags since they don't actually "start with <i>
", as the criterion you mentioned.â J. M. is computer-lessâ¦
27 mins ago
@J.M.iscomputer-less That's actually perfect ! Maybe I was not clear enough, but I don't want to catch "<i>" where the sentence does not start with "<i>". Can you post it as an answer ? Then I can accept it, if you also know how to replace the "<i>" and "</i>" in the found sentences.
â james
23 mins ago
@J.M.iscomputer-less That's actually perfect ! Maybe I was not clear enough, but I don't want to catch "<i>" where the sentence does not start with "<i>". Can you post it as an answer ? Then I can accept it, if you also know how to replace the "<i>" and "</i>" in the found sentences.
â james
23 mins ago
If just for replacement, it seems no need to get their positions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
22 mins ago
If just for replacement, it seems no need to get their positions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
22 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó Okay, interesting. How would you do it then ?
â james
22 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó Okay, interesting. How would you do it then ?
â james
22 mins ago
You did not mention what kind of replacement you wanted to do. Is it derived from the matching string, or something completely different?
â J. M. is computer-lessâ¦
20 mins ago
You did not mention what kind of replacement you wanted to do. Is it derived from the matching string, or something completely different?
â J. M. is computer-lessâ¦
20 mins ago
 |Â
show 2 more comments
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Cases[text, _String?(StringMatchQ["<i>*"]), Infinity]
"aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.", "aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0."
StringReplace[text,
StartOfString ~~ "<i>" ~~ s__ ~~ "</i>" ~~ EndOfString :> "REPLACE" <> s <> "REPLACE"]
"Donc", "j'obtiens", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) +
aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢
= 0.REPLACE", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ -
òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"Donc en particulier", "ça veut dire que si je multiplie par
c", "pour tout nombre réel c"
Thanks a lot ! This works nicely and is comprehensible.
â james
11 mins ago
@james, my pleasure. Thank you for the accept.
â kglr
10 mins ago
add a comment |Â
up vote
2
down vote
For replacement, (e.g., below is just to delete them)
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "$1"]
The key point is to locate the (sub)strings (but not by knowing the positions explicitly), having achieved which, one can do whatever one wants to them :).
For the updated detailed request
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "REPLACE$1REPLACE"]
This replaces the "<i>" and "<i/>" but kills the rest of the text.
â james
17 mins ago
@jamesStringReplace
is better here, which leaves strings unmatched untouched. I modified it.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
16 mins ago
Thank you very much for your answer !
â james
11 mins ago
add a comment |Â
up vote
1
down vote
StringReplace["this is the <i>old text</i>" ,
"<i>" ~~ ___ ~~ "</i>" -> "new text"]
This replaces everything in between "<i>" and "<i/>". Please have a look at my example in the question.
â james
14 mins ago
That's what I was asking you about earlier @james. So, what you want is a derived string and not an independent string. That's because replacing<i>stuff</i>
withstuff
and replacing<i>stuff</i>
withsomething
needs different approaches.
â J. M. is computer-lessâ¦
12 mins ago
@J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information.
â james
9 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Cases[text, _String?(StringMatchQ["<i>*"]), Infinity]
"aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.", "aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0."
StringReplace[text,
StartOfString ~~ "<i>" ~~ s__ ~~ "</i>" ~~ EndOfString :> "REPLACE" <> s <> "REPLACE"]
"Donc", "j'obtiens", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) +
aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢
= 0.REPLACE", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ -
òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"Donc en particulier", "ça veut dire que si je multiplie par
c", "pour tout nombre réel c"
Thanks a lot ! This works nicely and is comprehensible.
â james
11 mins ago
@james, my pleasure. Thank you for the accept.
â kglr
10 mins ago
add a comment |Â
up vote
2
down vote
accepted
Cases[text, _String?(StringMatchQ["<i>*"]), Infinity]
"aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.", "aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0."
StringReplace[text,
StartOfString ~~ "<i>" ~~ s__ ~~ "</i>" ~~ EndOfString :> "REPLACE" <> s <> "REPLACE"]
"Donc", "j'obtiens", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) +
aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢
= 0.REPLACE", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ -
òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"Donc en particulier", "ça veut dire que si je multiplie par
c", "pour tout nombre réel c"
Thanks a lot ! This works nicely and is comprehensible.
â james
11 mins ago
@james, my pleasure. Thank you for the accept.
â kglr
10 mins ago
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Cases[text, _String?(StringMatchQ["<i>*"]), Infinity]
"aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.", "aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0."
StringReplace[text,
StartOfString ~~ "<i>" ~~ s__ ~~ "</i>" ~~ EndOfString :> "REPLACE" <> s <> "REPLACE"]
"Donc", "j'obtiens", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) +
aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢
= 0.REPLACE", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ -
òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"Donc en particulier", "ça veut dire que si je multiplie par
c", "pour tout nombre réel c"
Cases[text, _String?(StringMatchQ["<i>*"]), Infinity]
"aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.", "aáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0."
StringReplace[text,
StartOfString ~~ "<i>" ~~ s__ ~~ "</i>" ~~ EndOfString :> "REPLACE" <> s <> "REPLACE"]
"Donc", "j'obtiens", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) +
aáµ¢âÂÂ(ñâ - òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢
= 0.REPLACE", "REPLACEaáµ¢âÂÂ(ñâ - òâÂÂ) + aáµ¢âÂÂ(ñâ -
òâÂÂ) + ... + aâÂÂâÂÂ(ñâ - òâÂÂ) = báµ¢ - báµ¢ = 0.REPLACE",
"Donc en particulier", "ça veut dire que si je multiplie par
c", "pour tout nombre réel c"
edited 15 mins ago
answered 20 mins ago
kglr
165k8188388
165k8188388
Thanks a lot ! This works nicely and is comprehensible.
â james
11 mins ago
@james, my pleasure. Thank you for the accept.
â kglr
10 mins ago
add a comment |Â
Thanks a lot ! This works nicely and is comprehensible.
â james
11 mins ago
@james, my pleasure. Thank you for the accept.
â kglr
10 mins ago
Thanks a lot ! This works nicely and is comprehensible.
â james
11 mins ago
Thanks a lot ! This works nicely and is comprehensible.
â james
11 mins ago
@james, my pleasure. Thank you for the accept.
â kglr
10 mins ago
@james, my pleasure. Thank you for the accept.
â kglr
10 mins ago
add a comment |Â
up vote
2
down vote
For replacement, (e.g., below is just to delete them)
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "$1"]
The key point is to locate the (sub)strings (but not by knowing the positions explicitly), having achieved which, one can do whatever one wants to them :).
For the updated detailed request
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "REPLACE$1REPLACE"]
This replaces the "<i>" and "<i/>" but kills the rest of the text.
â james
17 mins ago
@jamesStringReplace
is better here, which leaves strings unmatched untouched. I modified it.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
16 mins ago
Thank you very much for your answer !
â james
11 mins ago
add a comment |Â
up vote
2
down vote
For replacement, (e.g., below is just to delete them)
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "$1"]
The key point is to locate the (sub)strings (but not by knowing the positions explicitly), having achieved which, one can do whatever one wants to them :).
For the updated detailed request
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "REPLACE$1REPLACE"]
This replaces the "<i>" and "<i/>" but kills the rest of the text.
â james
17 mins ago
@jamesStringReplace
is better here, which leaves strings unmatched untouched. I modified it.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
16 mins ago
Thank you very much for your answer !
â james
11 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
For replacement, (e.g., below is just to delete them)
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "$1"]
The key point is to locate the (sub)strings (but not by knowing the positions explicitly), having achieved which, one can do whatever one wants to them :).
For the updated detailed request
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "REPLACE$1REPLACE"]
For replacement, (e.g., below is just to delete them)
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "$1"]
The key point is to locate the (sub)strings (but not by knowing the positions explicitly), having achieved which, one can do whatever one wants to them :).
For the updated detailed request
StringReplace[text, RegularExpression["^<i>(.*)</i>$"] -> "REPLACE$1REPLACE"]
edited 5 mins ago
answered 20 mins ago
ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2,5241826
2,5241826
This replaces the "<i>" and "<i/>" but kills the rest of the text.
â james
17 mins ago
@jamesStringReplace
is better here, which leaves strings unmatched untouched. I modified it.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
16 mins ago
Thank you very much for your answer !
â james
11 mins ago
add a comment |Â
This replaces the "<i>" and "<i/>" but kills the rest of the text.
â james
17 mins ago
@jamesStringReplace
is better here, which leaves strings unmatched untouched. I modified it.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
16 mins ago
Thank you very much for your answer !
â james
11 mins ago
This replaces the "<i>" and "<i/>" but kills the rest of the text.
â james
17 mins ago
This replaces the "<i>" and "<i/>" but kills the rest of the text.
â james
17 mins ago
@james
StringReplace
is better here, which leaves strings unmatched untouched. I modified it.â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
16 mins ago
@james
StringReplace
is better here, which leaves strings unmatched untouched. I modified it.â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
16 mins ago
Thank you very much for your answer !
â james
11 mins ago
Thank you very much for your answer !
â james
11 mins ago
add a comment |Â
up vote
1
down vote
StringReplace["this is the <i>old text</i>" ,
"<i>" ~~ ___ ~~ "</i>" -> "new text"]
This replaces everything in between "<i>" and "<i/>". Please have a look at my example in the question.
â james
14 mins ago
That's what I was asking you about earlier @james. So, what you want is a derived string and not an independent string. That's because replacing<i>stuff</i>
withstuff
and replacing<i>stuff</i>
withsomething
needs different approaches.
â J. M. is computer-lessâ¦
12 mins ago
@J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information.
â james
9 mins ago
add a comment |Â
up vote
1
down vote
StringReplace["this is the <i>old text</i>" ,
"<i>" ~~ ___ ~~ "</i>" -> "new text"]
This replaces everything in between "<i>" and "<i/>". Please have a look at my example in the question.
â james
14 mins ago
That's what I was asking you about earlier @james. So, what you want is a derived string and not an independent string. That's because replacing<i>stuff</i>
withstuff
and replacing<i>stuff</i>
withsomething
needs different approaches.
â J. M. is computer-lessâ¦
12 mins ago
@J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information.
â james
9 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
StringReplace["this is the <i>old text</i>" ,
"<i>" ~~ ___ ~~ "</i>" -> "new text"]
StringReplace["this is the <i>old text</i>" ,
"<i>" ~~ ___ ~~ "</i>" -> "new text"]
answered 15 mins ago
LouisB
4,1941616
4,1941616
This replaces everything in between "<i>" and "<i/>". Please have a look at my example in the question.
â james
14 mins ago
That's what I was asking you about earlier @james. So, what you want is a derived string and not an independent string. That's because replacing<i>stuff</i>
withstuff
and replacing<i>stuff</i>
withsomething
needs different approaches.
â J. M. is computer-lessâ¦
12 mins ago
@J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information.
â james
9 mins ago
add a comment |Â
This replaces everything in between "<i>" and "<i/>". Please have a look at my example in the question.
â james
14 mins ago
That's what I was asking you about earlier @james. So, what you want is a derived string and not an independent string. That's because replacing<i>stuff</i>
withstuff
and replacing<i>stuff</i>
withsomething
needs different approaches.
â J. M. is computer-lessâ¦
12 mins ago
@J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information.
â james
9 mins ago
This replaces everything in between "<i>" and "<i/>". Please have a look at my example in the question.
â james
14 mins ago
This replaces everything in between "<i>" and "<i/>". Please have a look at my example in the question.
â james
14 mins ago
That's what I was asking you about earlier @james. So, what you want is a derived string and not an independent string. That's because replacing
<i>stuff</i>
with stuff
and replacing <i>stuff</i>
with something
needs different approaches.â J. M. is computer-lessâ¦
12 mins ago
That's what I was asking you about earlier @james. So, what you want is a derived string and not an independent string. That's because replacing
<i>stuff</i>
with stuff
and replacing <i>stuff</i>
with something
needs different approaches.â J. M. is computer-lessâ¦
12 mins ago
@J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information.
â james
9 mins ago
@J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information.
â james
9 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183972%2freplacing-strings-at-specific-locations%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Could try something like
Position[text, s_String /; StringMatchQ[s, "<i>*</i>"]]
, but that won't catch the last two items with italics tags since they don't actually "start with<i>
", as the criterion you mentioned.â J. M. is computer-lessâ¦
27 mins ago
@J.M.iscomputer-less That's actually perfect ! Maybe I was not clear enough, but I don't want to catch "<i>" where the sentence does not start with "<i>". Can you post it as an answer ? Then I can accept it, if you also know how to replace the "<i>" and "</i>" in the found sentences.
â james
23 mins ago
If just for replacement, it seems no need to get their positions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
22 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó Okay, interesting. How would you do it then ?
â james
22 mins ago
You did not mention what kind of replacement you wanted to do. Is it derived from the matching string, or something completely different?
â J. M. is computer-lessâ¦
20 mins ago