Jump to content

preg_match || General problem with REGEX


jimmydorry

Recommended Posts

I can't seem to get this OR statement working... it's the '|' character.

 

expression:

'%\](.*)(\(|\[)%mis'

 

string:

'[HnG] DD Hokuto no Ken 02 (TV) [8BC696B8].​avi'

 

result:

' DD Hokuto no Ken 02 (TV) '

 

expected result:

' DD Hokuto no Ken 02 '

Link to comment
Share on other sites

try '%\](.*?)(\(|\[)%mis'

Wow, it bloody works. Would you mind explaining what the '?' does, and how you knew to use it?

 

Thanks a heap man.

 

EDIT: From what I understand, it means that the phrase can occur 0 or 1 times.

 

? makes the .* "ungreedy", so it doesn't match the ( or [.  Alternate way (untested):

 

'%\][^\(\[]+[\(\[]%i'

 

Not sure if you needed the capturing parentheses but I removed them.

Link to comment
Share on other sites

try '%\](.*?)(\(|\[)%mis'

Wow, it bloody works. Would you mind explaining what the '?' does, and how you knew to use it?

 

Thanks a heap man.

 

EDIT: From what I understand, it means that the phrase can occur 0 or 1 times.

 

? makes the .* "ungreedy", so it doesn't match the ( or [.  Alternate way (untested):

 

'%\][^\(\[]+[\(\[]%i'

 

Not sure if you needed the capturing parentheses but I removed them.

I understand your alternate solution better, and will have to try it later. Thanks for the explanations though, and for both solutions.

 

If I understand correctly, the alternate solution utilises the regex operator of NOT EQUAL TO. Thus it is saying grab everything after ']' that is not '(' OR '['.

Link to comment
Share on other sites

I understand your alternate solution better, and will have to try it later. Thanks for the explanations though, and for both solutions.

 

If I understand correctly, the alternate solution utilises the regex operator of NOT EQUAL TO. Thus it is saying grab everything after ']' that is not '(' OR '['.

 

Yes.  I'm not sure if it makes a difference but I tend to use character classes [] a lot.

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.