Jump to content

Find the position of the first occuring number


jimmydorry

Recommended Posts

Problem: I need to find the position of the first occuring integer after the first hypen. Help would be appreciated.

 

Proposed Pattern:

'%([\d]+)%'

 

Test Strings:

'[HorribleSubs] Bleach – 311 [720p].mkv'

'[NARUTO-BASE.RU] Bleach – 311 [720p]_[ENG].mkv'

 

Result:

N/A (I have no idea how we can get the string position in this situation... I was thinking of extracting the integer with the above code, and then doing a strpos method to find where it starts... but that isn't very elegant. I was also not too sure on how to find the first number occuring after a hyphen. You may also notice that the second string has two hypens before the first number, so I will need to be able to handle that.)

 

Expected Result:

~24 (I'm bad at counting  :D)

Link to comment
Share on other sites

If you are using PHP and preg_match() then PREG_OFFSET_CAPTURE.  May be something similar in other languages.

Could you give me an example?

 

I don't see what you mean by PREG_OFFSET_CAPTURE.

 

I should have written this was in php, but I assumed since I posted it here people would assume it was.

Link to comment
Share on other sites

That's a flag passed to preg_match() that will return the offset (position) in the matches.  For a regex we'll need to know what is common in the strings (do they always start [something]?  Does the number always occur after a dash and space ( - )?

Link to comment
Share on other sites

preg_match('~^[^\]]*[^–-]*[^0-9]*([0-9])~',$string,$pos,PREG_OFFSET_CAPTURE);
$pos = $pos[1][1];

I understand what is meant now. I just need to formulate the REGEX now.

 

I tried the pattern above on this site: http://www.pagecolumn.com/tool/pregtest.htm ... since I am not home right now. It did not work, but might do the job in a proper development environment.

 

When I get home, I will try adapting the pattern I first proposed.

 

That's a flag passed to preg_match() that will return the offset (position) in the matches.  For a regex we'll need to know what is common in the strings (do they always start [something]?  Does the number always occur after a dash and space ( - )?

The strings will all look like:

'[HorribleSubs] Bleach – 311 [720p].mkv'

'[NARUTO-BASE.RU] Bleach – 311 [720p]_[ENG].mkv'

 

Some might have multiple hyphens, but most will be like the first one with 1 hyphen. The numbers might not occur directly after the hyphen, as there might be a space... an underscore... multiple characters...etc.

 

I just need the first number that occurs after a hyphen... i.e. ignores the other hyphens and grabs the first number. It is likely that some strings might not have numbers. Is it possible to cater for that in the regex? Or can that only be done by checking the output in the php code?

Link to comment
Share on other sites

The regex tester link you posted does just that: test regexes against a string, and outputs matches.  That is not what you asked for.  You asked to have the character position of the first occurring digit after the hyphen, not counting what's there at the beginning (interpreted as "between the brackets").  The regex tester cannot give you that answer, because that is not what it does.  My code does what you asked for. 

 

As to your question about handling strings with no numbers, in my code example, if there is no number, $pos will return NULL.

 

 

Link to comment
Share on other sites

Thanks everyone for your contributions. I'm now up and running. Sorry, I didn't respond until now... I was preoccupied for the last two days.

 

The regex tester link you posted does just that: test regexes against a string, and outputs matches.  That is not what you asked for.  You asked to have the character position of the first occurring digit after the hyphen, not counting what's there at the beginning (interpreted as "between the brackets").  The regex tester cannot give you that answer, because that is not what it does.  My code does what you asked for. 

 

As to your question about handling strings with no numbers, in my code example, if there is no number, $pos will return NULL.

 

Thanks your expression does work in the proper environment, and so does the rest of your code. I was trying to test if the actual regex pattern you wrote worked (since I was at uni where I didn't have access to a proper dev environment)... and I can see from what you wrote why it failed now.

 

try

<?php

function my_ofset($text){
    preg_match('/^[^\-]*-\D*/', $text, $m);
    return strlen($m[0]);
}
echo my_ofset('[HorribleSubs] Bleach - 311 [720p].mkv');
?> 

And that is a very interesting way of doing it without the OFFSET_CAPTURE flag. Thanks for sharing.

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.