Question

I have a string like:

$str = "\textaolig 3 \texthtbardotlessjvar  \textrthooklong
    B \textbenttailyogh ; \textinvomega q \textscaolig
    . \textbktailgamma p \textinvsca r \textscdelta
    D \textctinvglotstop ! \textinvscripta s \textscf
    2 \textctjvar I \textlfishhookrlig t \textsck";

And I wanna get all the TeX commands (\textaolig etc.) with a regular expression. I tried:

\\([[a-z]\s]+)

But with no luck. Can someone help me out?

Kind regards,

Was it helpful?

Solution

First of all, you have to enclose your string with single quotes because when using double quotes if you use a backslash it will try to escape a character, which isn't your case.

Secondly, you have to use \\\\ to match a backslash.

I don't know about TeX commands, but I've tried to match [\w\s\.;]+ so correct it if needed:

$str = '\textaolig 3 \texthtbardotlessjvar \textrthooklong
    B \textbenttailyogh ; \textinvomega q \textscaolig
    . \textbktailgamma p \textinvsca r \textscdelta
    D \textctinvglotstop ! \textinvscripta s \textscf
    2 \textctjvar I \textlfishhookrlig t \textsck';
    preg_match_all('/\\\\(?P<tex>\w+)/s', $str, $m);

    var_dump($m["tex"]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top