Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.';


PHP
$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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900