3
$\begingroup$
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>"
$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$ Commented Oct 16, 2018 at 5:35
  • $\begingroup$ @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. $\endgroup$
    – james
    Commented Oct 16, 2018 at 5:38
  • $\begingroup$ If just for replacement, it seems no need to get their positions. $\endgroup$ Commented Oct 16, 2018 at 5:39
  • $\begingroup$ @ΑλέξανδροςΖεγγ Okay, interesting. How would you do it then ? $\endgroup$
    – james
    Commented Oct 16, 2018 at 5:40
  • $\begingroup$ You did not mention what kind of replacement you wanted to do. Is it derived from the matching string, or something completely different? $\endgroup$ Commented Oct 16, 2018 at 5:42

3 Answers 3

5
$\begingroup$
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"}

$\endgroup$
1
  • $\begingroup$ Thanks a lot ! This works nicely and is comprehensible. $\endgroup$
    – james
    Commented Oct 16, 2018 at 5:51
4
$\begingroup$

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"]
$\endgroup$
2
  • $\begingroup$ This replaces the "<i>" and "<i/>" but kills the rest of the text. $\endgroup$
    – james
    Commented Oct 16, 2018 at 5:44
  • $\begingroup$ @james StringReplace is better here, which leaves strings unmatched untouched. I modified it. $\endgroup$ Commented Oct 16, 2018 at 5:46
2
$\begingroup$
StringReplace["this is the <i>old text</i>" , 
 "<i>" ~~ ___ ~~ "</i>" -> "new text"]
$\endgroup$
3
  • $\begingroup$ This replaces everything in between "<i>" and "<i/>". Please have a look at my example in the question. $\endgroup$
    – james
    Commented Oct 16, 2018 at 5:48
  • $\begingroup$ 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. $\endgroup$ Commented Oct 16, 2018 at 5:50
  • $\begingroup$ @J.M.iscomputer-less I see. I guess I miscommunicated my answer. Thank you for the information. $\endgroup$
    – james
    Commented Oct 16, 2018 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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