jimmydorry Posted March 2, 2011 Share Posted March 2, 2011 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/ Share on other sites More sharing options...
AbraCadaver Posted March 2, 2011 Share Posted March 2, 2011 If you are using PHP and preg_match() then PREG_OFFSET_CAPTURE. May be something similar in other languages. Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/#findComment-1181990 Share on other sites More sharing options...
jimmydorry Posted March 2, 2011 Author Share Posted March 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/#findComment-1181994 Share on other sites More sharing options...
AbraCadaver Posted March 2, 2011 Share Posted March 2, 2011 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 ( - )? Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/#findComment-1181998 Share on other sites More sharing options...
.josh Posted March 3, 2011 Share Posted March 3, 2011 preg_match('~^[^\]]*[^–-]*[^0-9]*([0-9])~',$string,$pos,PREG_OFFSET_CAPTURE); $pos = $pos[1][1]; Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/#findComment-1182190 Share on other sites More sharing options...
jimmydorry Posted March 3, 2011 Author Share Posted March 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/#findComment-1182206 Share on other sites More sharing options...
.josh Posted March 3, 2011 Share Posted March 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/#findComment-1182210 Share on other sites More sharing options...
sasa Posted March 3, 2011 Share Posted March 3, 2011 try <?php function my_ofset($text){ preg_match('/^[^\-]*-\D*/', $text, $m); return strlen($m[0]); } echo my_ofset('[HorribleSubs] Bleach - 311 [720p].mkv'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/#findComment-1182218 Share on other sites More sharing options...
jimmydorry Posted March 6, 2011 Author Share Posted March 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229406-find-the-position-of-the-first-occuring-number/#findComment-1183455 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.