Jump to content

Recommended Posts

Ok, so I'm getting a little better at regex then when I first started (Before I knew what .*? mean XD) and I'm wondering this. Say I use

 

preg_math('/[0-9]*? ([A-Za-z]*)/', $SomeStrings, $Matches);

 

On a series of strings where it matches that, how do I get it to NOT have the [A-Za-z] part in $Matches?

Link to comment
https://forums.phpfreaks.com/topic/168390-dont-return-parathesis-content/
Share on other sites

that can be interpreted at least 3 different ways.  I heard a rumor that the more specific you are about your question, the more likely people are going to (?:be able to)? help you.

 

1) match but not capture

change that part to (?:[A-Za-z*)

 

2) match anything but that (negated char class)

change that part to ([^A-Za-z*)

 

3) not look for it at all

remove it from your pattern

 

 

Simply remove the parentheses. They're the ones capturing the match. If you need to group part of the pattern without capturing the match (e.g. when using alternations), you can use non-capturing parentheses (?:pattern)

 

And CV beat me to it, but in turn I didn't interpret it in more than one way. lol

Awesome. Next question. Say I have

 

preg_match('/(?:Some ([0-9]*) Text)|(?[0-9]*) Some Text)/', $SomeString, $Matches);

 

it'll match Some 11 Text or 11 Some Text, but if it's Some 11 Text, the 11 with be stored in $Matches[1] while if it's 11 Some Text, the 11 will be stored in $Matches[2] and $Matches[1] will be blank. How do I make it so that the 11 in Some 11 Text will only back reference if that the rest of the string in (?:Some ([0-9]*) Text) matches. So basically I want 11 to be stored in $Matches[1] for both strings

You know, I'm also wondering if we are making this more complicated than it needs to be.  Is the goal here to extract a number from a string, regardless of where that number is? Is there a reason you can't just do

 

/[0-9]+/

 

?

 

It would help if you actually posted an example of the actual content.

This is my actual code

 

<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

$NamePattern = '/(?:\[DATS\] Ryuusei no Rockman \- ([0-9]{1,})(?:v2)? (?:\[FINAL\])?\[XviD\]\[[0-9a-zA-Z]*\])|(?[0-9]*))/';
$WikiPattern = '';

//preg_match_all($WikiPattern, $result, $WikiMatch);

//Put these into an array here

//Open images directory
$dir = dir("Wiki");

//List files in images directory
while (($file = $dir->read()) !== false)
{
if($file == "." || $file == "..")
	{
	continue;
	}

if(preg_match($NamePattern, $file, $M))
	{
	//echo $file;
	print_r($M);
	}

else
	{
	//echo "<s>" . $file . "</s>";
	}
echo "<br>";
}

$dir->close();

//echo $result;
?>

 

Basically, it's meant to take all the file names of downloaded anime, which I have yet to rename, fetch a page with the list of their episode names (going to put that in later) and then assign those names to them automatically based on the episode number in the file name. Here are the names that I have to work with

 

01 Fugitive War-Rock.avi
02 First Contact.avi
03 Electromagnetic Wave Virus.avi
04 Electromagnetic Wave Conversion.avi
05 Subaru, On Air.avi
06 Cygnus's Challenge.avi
07 Satellite Crash.avi
08 Ox Invasion.avi
09 Rampaging Ox Fire.avi
10 Meet Harp Singer.avi
11 Harp Note's Surprise Debut.avi
12 A Virus Human Appears.avi
13 Ikuta-sensei's Science Classroom.avi
14 Libra Ikuta's Dangerous Classroom.avi
15 Laughing Electromagnetic Wave Space.avi
16 Angry Electromagnetic Wave Space.avi
17 Singing Electromagnetic Wave Space.avi
18 Calling on Cancer buku.avi
19 Rockman Express Mail.avi
20 The Gardener Howls at the Moon.avi
21 Wolf Howl of Anger.avi
22 Electromagnetic Wave Ghost.avi
23 The Immortal Suicidal Spirit.avi
24 Ophiuchus Queen.avi
26.avi
27.avi
28.avi
29.avi
30.avi
31.avi
32.avi
[DATS] Ryuusei no Rockman - 33 [XviD][2E374B05].avi
[DATS] Ryuusei no Rockman - 34 [XviD][A869569C].avi
[DATS] Ryuusei no Rockman - 35 [XviD][777797A1].avi
[DATS] Ryuusei no Rockman - 36 [XviD][7FB182DA].avi
[DATS] Ryuusei no Rockman - 37 [XviD][bB645804].avi
[DATS] Ryuusei no Rockman - 38 [XviD][534E169B].avi
[DATS] Ryuusei no Rockman - 39 [XviD][71C26B0A].avi
[DATS] Ryuusei no Rockman - 40 [XviD][E8774151].avi
[DATS] Ryuusei no Rockman - 41 [XviD][121BE67D].avi
[DATS] Ryuusei no Rockman - 42 [XviD][070D4F4A].avi
[DATS] Ryuusei no Rockman - 43 [XviD][93F78554].avi
[DATS] Ryuusei no Rockman - 44 [XviD][A3E43FB1].avi
[DATS] Ryuusei no Rockman - 45v2 [XviD][FD6D58FD].avi
[DATS] Ryuusei no Rockman - 46v2 [XviD][067CE1AA].avi
[DATS] Ryuusei no Rockman - 47v2 [XviD][146B6E50].avi
[DATS] Ryuusei no Rockman - 48v2 [XviD][A5C31B9D].avi
[DATS] Ryuusei no Rockman - 49v2 [XviD][31D7450D].avi
[DATS] Ryuusei no Rockman - 50 [XviD][2F5AE125].avi
[DATS] Ryuusei no Rockman - 51 [XviD][F5CB15F7].avi
[DATS] Ryuusei no Rockman - 52 [XviD][E4C9510B].avi
[DATS] Ryuusei no Rockman - 53 [XviD][6BC4C683].avi
[DATS] Ryuusei no Rockman - 54 [XviD][0F0A550D].avi
[DATS] Ryuusei no Rockman - 55 [FINAL][XviD][00A4641C].avi

 

I might be making it a little too complicated, but basically I want it to be a flexible script, so that I can use it to rename any TV Show or Anime automatically by just changing the Patterns.

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.