Hello, there is a list of strings:

"number: что угодно" "что угодно 2" "number: что угодно 3" 

It is necessary to get such a result in regular intervals:

 "что угодно" "что угодно 2" "что угодно 3" 

Such an example does not help me:

 preg_match('/:?\s*(.+)/', 'number: что угодно', $matches) 

    5 answers 5

     preg_match('/^(?:.*:)?\s*(.+)/', 'number что угодно', $matches); 

    We declare as optional all characters from the beginning of the line to the colon, so when the colon is allocated the line after it, and when there is no colon, we don’t expect a single character at all.

      Alternatively: in a group without feedback, first try to loop through all the characters to the colon, the colon itself and the non-mandatory spaces following it (it is probably worth replacing \s with \x20 ), and then choose what you need:

       /(?:.*?:\s*)?(.+)/ 

        Try .*?(что угодно .*) . Should help.

        • Asterisks gobbled up. Pomle points - asterisk - Smithson
        • Returned asterisks. - ߊߚߤߘ
        • Thank you, I'm from the tablet, it's uncomfortable to do markup. - Smithson
         $newSubject = preg_replace("#^(?:.+:)?\s*(.+)\s*$#", "$1", $subject); 

        if present, all non-colon characters are truncated in the passive group (?:) , also, all characters that are not in the group between the supposed spaces are truncated. at the output we have only a string that falls into the group, on which the original string changes

        example:

         $ cat example.php <?php foreach ([ "number: что угодно", "что угодно 2", "number: что угодно 3" ] as $subject) { $newSubject = preg_replace("#^(?:.+:)?\s*(.+)\s*$#", "$1", $subject); echo "$newSubject\n"; } $ php example.php что угодно что угодно 2 что угодно 3 
           preg_match('/^(?:\s*number:\s*)?\s*(.+)$/', 'number: что угодно', $matches); 
          • write more expanded - Naumov
          • one
            Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky