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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.