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:

  1. How can I get the position of in the list of every object that starts with <i>?

  2. 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>"
  • 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 Oct 16 at 5:35
  • @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 Oct 16 at 5:38
  • If just for replacement, it seems no need to get their positions. – Αλέξανδρος Ζεγγ Oct 16 at 5:39
  • @ΑλέξανδροςΖεγγ Okay, interesting. How would you do it then ? – james Oct 16 at 5:40
  • 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 Oct 16 at 5:42
up vote 4 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 Oct 16 at 5:51

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 Oct 16 at 5:44
  • @james StringReplace is better here, which leaves strings unmatched untouched. I modified it. – Αλέξανδρος Ζεγγ Oct 16 at 5:46
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 Oct 16 at 5:48
  • 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 Oct 16 at 5:50
  • @J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information. – james Oct 16 at 5:52

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.