Jump to content

Damn you regex...


ikin

Recommended Posts

Well I have never learn'd Regex, so I need some help with it.

I'm trying to remove the |file = in the string (the code is in wikipedia format):

{{Infobox Music
|name           = Body Parts
|release        = [[31 January]] [[2005]]
|update         = Creature of Fenkenstrain
|members        = Yes
|location       = Experiment Dungeon
|skillrequired  = 
|questrequired  = [[Creature of Fenkenstrain]]
|hint           = This track unlocks at the dungeon south-east of Fenkenstrain's Castle.
|file = 
}}

oh if it were just to do str_replace it would be love but no, they have to use multi spaces sometimes :@...

Like:

|      file     =

Etc but only spaces nothing else,

So I was trying to replace it (as I said):

echo(preg_replace("/(\|)(\s+)(\file)(\s+)(\=)/i", "", $e));

But it didn't work (Yes I tried with many...)

So I come here to ask for help.

Thanks for reading.

Link to comment
https://forums.phpfreaks.com/topic/217732-damn-you-regex/
Share on other sites

You were nearly there. \f means something special when within a double-quoted string, just remove that slash and it would have worked. 

 

For what it's worth, you also (obviously, after JAY6390's post) need any of the parentheses nor a slash before the equals character.  It would also be good to "anchor" the expression so that the | can only be matched at the start of a line. Also, read up on the m pattern modifier that JAY6390 introduced and see if you think it is necessary or not.

Link to comment
https://forums.phpfreaks.com/topic/217732-damn-you-regex/#findComment-1130201
Share on other sites

I did actually mean to put in the ^ anchor, oops. True the | doesn't need a \ before it, but only if the ^ is before it. if left without the ^ anchor and no \ before the |, it won't be a valid regex. I generally escape all special characters just out of habit to be honest, even when not needed inside character classes etc

Link to comment
https://forums.phpfreaks.com/topic/217732-damn-you-regex/#findComment-1130202
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.