Click here to Skip to main content
14,272,370 members
Rate this:
Please Sign up or sign in to vote.
Example
$s = 'This is a example string with some testing cases \cite{author1,author2,author3}.
Then {another example} \citeauthor{author3,author4} and finally
\citeP{author1} \citesome text{author5} \citesome text with[.]$"{author6}and string completes here.';


$output= preg_replace_callback('/\\\\(cite(?:\w+)?){([^}]*)}/', function($m) {
  return preg_replace('/([^,]+)(?:,|$)/' ,'<xref '. $m[1] . ' >$1</xref>', $m[2]); },
  $s);


Output:

This is a example string with some testing cases <xref cite="">author1<xref cite="">author2<xref cite="">author3.
Then {another example} <xref citeauthor="">author3<xref citeauthor="">author4 and finally
<xref citep="">author1 \citesome text{author5} \citesome text with[.]$"{author6}and string completes here.

Expected output:

This is a example string with some testing cases <xref cite >author1</xref><xref cite >author2</xref><xref cite >author3</xref>. 
Then {another example} <xref citeauthor >author3</xref><xref citeauthor >author4</xref> and finally
<xref citeP >author1</xref>  <xref citesome text>author5</xref> <xref citesome text with[.]$">author6</xref>and string completes here.


Question:
/\\\\(cite(?:\w+)?)
- only get group of words. I need to get group of words with space symbols special character everything. What would be the regex
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100