使用正则表达式替换第二次出现的子字符串

I have a string with xml formatted like what is shown below.

How can I find the second occurrence of <para> and replace with <para align='left'>?

$str = "<para>From the New York Times</para>

<para>Instacart, a two-year-old grocery delivery company, announced a $44 million round of financing on Monday led by Andreessen Horowitz. Three venture capital firms that previously invested in the company, Sequoia Capital, Khosla Ventures and Canaan Partners, participated in the latest round.</para>

<para>The company, which is based in San Francisco, lets customers shop online from grocery stores in their area. The orders are filled by other people who have signed up to be shoppers and who receive a cut of the delivery fees. Information about a store’s inventory comes from store managers and from the shoppers. The company says it can have groceries delivered within an hour.</para>";

Use a DOM parser with an XPath expression to achieve this. The expression //para[2] will select just the second <para> node. Once you have selected the required node, you can simply alter it however you wish, using the set of available functions that DOMDocument offers. In this case, you can simply use setAttribute() to set the attribute align with a value of left.

$dom = new DOMDocument;

libxml_use_internal_errors(true); // Disable error reporting
$dom->loadHTML($str);

$xpath = new DOMXPath($dom);

$secondPara = $xpath->query('//para[2]');
$secondPara->item(0)->setAttribute('align', 'left');

echo $dom->saveHTML();

With this regex (see demo):

(?s)\A.*?<para>.*?\K<para>

In your php code:

$regex = "~(?s)\A.*?<para>.*?\K<para>~";
$replaced = preg_replace($regex,"<para align='left'>",$string);

Explain Regex

(?s)                     # set flags for this block (with . matching
                         # 
) (case-sensitive) (with ^ and $
                         # matching normally) (matching whitespace
                         # and # normally)
\A                       # the beginning of the string
.*?                      # any character (0 or more times (matching
                         # the least amount possible))
<para>                   # '<para>'
.*?                      # any character (0 or more times (matching
                         # the least amount possible))
\K                       # 'Keep out:' Abandon what was matched so far.
<para>                   # '<para>'