0

I am facing a problem i am not capable to solve. I have a string consisting of not needed text and 10 digit numbers who always start with "2" or "6". I need to get those in 10digit numbers into an array. I thought of regex and found this article Regular Expression for matching a numeric sequence? which is pretty close to what i need (except the descending/ascending thing) yet, as i could never and will NEVER be able to understand regex, i cant modify to my needs. If anyone could help me out here i would highly appreciate it!

Here is a sample of my string:

".........693 7098469 - ZQH X Bop. Hrtepou 50 flerpoUrroXn ........210 5014166 - 0E000PA E KapaoAn Anpn-rPou 21
EAArivtg .....................................................210 9618677 - MAPIA KapaoAri Arpn-rptou 21 Elanvolo .. 210 9643623 - MAPIA E ...................................................... 210 9643887 - MAPIA 0 loucrrivou 8 HX.toOrran ..............210 9914534 AIPITAKHE APTEMIOE n Avrtnopou 22
Reptcrrept ....._.........._......._................697 7440896 , -10AN."

Thank you very much in advance! Greetings from Greece!

9
  • 1
    Thanks for the edit... i couldnt get the damn code block in there! :-) Commented Jan 6, 2014 at 10:10
  • 1
    Okay.. what have you tried? Show us what you've attempted and why it didn't work. Commented Jan 6, 2014 at 10:10
  • And what is your expected output? Commented Jan 6, 2014 at 10:11
  • 1
    @anubhava, All the numbers like 693 7098469 , 210 5014166 , etc.. the numbers that start with 2 or 6. Commented Jan 6, 2014 at 10:13
  • 1
    You CAN do this reg exp. It's almost simpler than logging into your OS... "I can't do regex" sounds like an excuse to have others do your work. Commented Jan 6, 2014 at 10:20

3 Answers 3

4

As I see your string your digits have an space between, and if you want strictly make your selections this is the regex:

[62]\d{2}\s*\d{7}

Explanation:

[62]    # Start with 6 or 2
\d{2}   # 2 more digits
\s*     # any number of white spaces
\d{7}   # 7 more digits

Live demo

and PHP code which has preg_match_all to match all occurrences of those strings:

preg_match_all("/[62]\d{2}\s*\d{7}/", $text, $matches);

Output:

Array
(
    [0] => 693 7098469
    [1] => 210 5014166
    [2] => 210 9618677
    [3] => 210 9643623
    [4] => 210 9643887
    [5] => 210 9914534
    [6] => 697 7440896
)

PHP live demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the explanation! I really think regex will be my nemesis for ever....
@JimBadanis-Kapirnas Beside its regular name - Regular Expressions - it's so nasty!
3

Maybe like this:

<?php
$x=
".........693 7098469 - ZQH X Bop. Hrtepou 50 flerpoUrroXn ........210 5014166 - 0E000PA E KapaoAn Anpn-rPou 21     EAArivtg  ....................................................210 9618677 - MAPIA KapaoAri Arpn-rptou 21 Elanvolo .. 210 9643623 - MAPIA E ...................................................... 210 9643887 - MAPIA 0 loucrrivou 8 HX.toOrran ..............210 9914534 AIPITAKHE APTEMIOE n Avrtnopou 22
Reptcrrept ....._.........._......._................697 7440896 , -10AN.";
$x=str_replace(' ','',$x);
preg_match_all('/((2|6)\d{9})/',$x,$matches);
print_r($matches[0]);

And the result:

Array
(
    [0] => 6937098469
    [1] => 2105014166
    [2] => 2109618677
    [3] => 2109643623
    [4] => 2109643887
    [5] => 2109914534
    [6] => 6977440896
)

Comments

2

there is a pretty cool page, that visualize the regex code for better understading: https://www.debuggex.com/

this should work

((?:2|6)[0-9]{2} [0-9]{7})

Regular expression visualization

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.