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?
\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.
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.
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.
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 whitespacesIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With