For a file names that were changed, I need regex to find all possibilities of the letters and numbers with any number of special characters between them. It is not consistent. The non word characters pop up any where in the string. For example, a file name like this:
180802_#[-_-]Myron-Dan_-_Truck#Lift.mp4
Got renamed to
180802MyronDanTruckLift.mp4
To find the files in my database, I need to use Regex which finds the text no matter what special characters are there. This works:
1(.*)8(.*)0(.*)8(.*)0(.*)2(.*)M(.*)y(.*)r(.*)o(.*)n(.*)D(.*)a(.*)n(.*)T(.*)r(.*)u(.*)c(.*)k(.*)L(.*)e(.*)t(.*)t(.*)e(.*)r(.*)
However, I know this is ridiculous and there has to be a better way. Could someone please suggest a better way to do this with Regex? Thanks
Oh I think it is important to add we create the current Regex string with a loop. Not essential but we do understand enough JavaScript to do that. Thanks
You can just use
[a-zA-Z0-9]
Hi, thanks for the reply. I don’t think I was clear that I am trying to match the exact string even though it has the special characters. That is why every character is show trying to represent what could come in between. Your Regex would be good for any string with the unwanted characters. I realize I could be asking a lot. Thanks
I don’t understand anyways. Just do a RegEx search in your database. For example in mySQL you would do
SELECT * FROM table WHERE REGEXP_REPLACE(column, '^[a-zA-Z0-9]', '') = REGEXP_REPLACE(yourFileName, '^[a-zA-Z0-9]', '')
If the “extra characters” cannot contain a-zA-Z0-9, then Thallius is correct, but you have to invert the pattern in a replace, as they show in the database example. Replace anything NOT [a-zA-Z0-9] with the empty string.
That’s it, I caught that after Thallius’s last post. Super helpful both of you! Thanks
Super helpful. Thanks so much!