Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matching a space after extension PCRE (php)

Tags:

regex

php

pcre

I am trying to migrate a fairly large and quite old database, one of the columns consists of file names. The problem is that in this one field there can be multiple filenames seperated by a space. For example:

"Filename.mp3 file anem.mid fi le nam e.rm"

I was trying to split these string with preg_split(), the closest regex I could come up with is

/(?<=\.[\w]{3})(\s)/

I know that /(?<=\.[\w]+)(\s)/ would not work since in PCRE a lookbehind has to have a fixed width. And since this is a music DB there are unconventional extentions aswell.

Any suggestions?

like image 865
ElvisElvis Avatar asked Apr 20 '18 14:04

ElvisElvis


People also ask

How do you match a space in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

Can you have spaces in regex?

The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.

How do you check if a string matches a regex in PHP?

In PHP, you can use the preg_match() function to test whether a regular expression matches a specific string. Note that this function stops after the first match, so this is best suited for testing a regular expression more than extracting data.


1 Answers

You can use this regex for split:

~\.\w+\K\h+~

RegEx Demo

RegEx Details:

  • \.: Match literal dot
  • \w+: Match 1+ word characters
  • \K: Reset matched info (forget about match data)
  • \h+: Match 1+ horizontal whitespaces
like image 109
anubhava Avatar answered Nov 03 '22 17:11

anubhava