Regular expression for picking text part

Asked

Viewed 4,107 times

1

I have the following text::

17/7/2014 14:58:44

Finalizada

170848546

I need to get exactly the "Finished", it’s PHP, I can’t understand regular expressions, someone gives me a light?

I created the following:

/([0-9][0-9]):([0-9][0-9]):([0-9][0-9])\n[a-zA-Z]*/

But it didn’t work, remembering that each word is in a different line.

1 answer

4


If the default is time > text > number, test this regex /[0-9]{2}.*:[[0-6]{2}\s*(.*)\s*[0-9]{1,}/

Demo: https://ideone.com/PmGiuL

Code:

$input = '17/7/2014 14:58:44

Finalizada

170848546';


$regex = '/[0-9]{2}.*:[[0-6]{2}\s*(.*)\s*[0-9]{1,}/';
preg_match($regex, $input, $resposta);
echo 'O textto encontrado foi: '.$resposta[1]; // Dá Finalizada

Regex explained:

  • [0-9]{2} - any number between 0 and 9, two characters
  • \/ - a bar (escape not to be confused with the regex end flag)
  • .* - any character except line break
  • : - colon
  • [0-6]{2} - any number between 0 and 6, two characters
  • \s* - line break, n times
  • (.*) - here the interesting are the parentheses that follow capture
  • [0-9]{1,} - any number between 0 and 9, n characters

If this pattern repeats too often you can use preg_match_all instead of just preg_match and then the variable $resposta[1] will be an array with the found results.

  • 1

    I think I understood the question by its answer +1, and excludes my answer :p

  • 1

    Thank you so much! That’s exactly what it was.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.