Hello everyone, I can’t deal with regular expressions for a bit. There is a line like:

FORMTEXT Rybakov (surname) (blood type) (date of determination) FORMTEXT Vladimir FORMTEXT Vladislavovich (first name, patronymic) (rhesus) (doctor's signature) FORMTEXT

How can I get a surname, first name and patronymic from here using regular expressions via php? Accordingly, it should work: Rybakov Vladimir Vladislavovich.

    1 answer 1

    Search by keyword FORMTEXT , followed by a single space \s - after the space, the search word \w+ , followed by an alternative choice: either a bracket ( , or a space with the keyword \sFORMTEXT . To prevent keywords with spaces in the result of the sample , you can enclose them in the design back and forth looking checks: (?<=FORMTEXT\s) and (?=\(|\sFORMTEXT) respectively. The whole template will take the form: (?<=FORMTEXT\s)\w+(?=\(|\sFORMTEXT) , and with php everything will look like this:

     $str = 'FORMTEXT Рыбаков(фамилия)(группа крови)(дата определения) FORMTEXT Владимир FORMTEXT Владиславович(имя, отчество)(резус)(подпись врача) FORMTEXT'; $patt = '~(?<=FORMTEXT\s)\w+(?=\(|\sFORMTEXT)~u'; preg_match_all($patt, $str, $arr); $result = $arr[0] ?? false; echo join(' ', $result); 

    Result:

     Рыбаков Владимир Владиславович 
    • @ Bek1zo I added in response to casting the array to a string. - Edward