Help writting simple regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrVon
    New Member
    • Nov 2009
    • 6

    Help writting simple regex

    Hi, i trying to write regular expression that looking for phone number in text file, but only does it if there is "Phone: " in front of number on same line.

    Expression i created looks like this
    Code:
    (?:(?:\d{1,3}[-. ]?)?[(]?\d{2,4}(?:[-). ]|[)][ ]))?\d{1,4}[-. ]?\d{4}(?:[-/.* ]\d{1,5})?
    How i add "Phone: " check to it?
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    You can add any text in your regex that you expect to see.

    For instance, if in your case the "Phone:" is at the beginning of the line in question, you could do the following:

    Code:
    m/^Phone:/
    That will match lines that start with "Phone:". You can simply add your regex after it.

    Hope that helps.

    Regards,

    Jeff

    Comment

    • MrVon
      New Member
      • Nov 2009
      • 6

      #3
      It does not work for me, could you show me how full code will look like?

      Comment

      • MrVon
        New Member
        • Nov 2009
        • 6

        #4
        The thing is that i actually use program that supports perl 5 programming language regular expressions, so could you please help me just add to my line of code check for "Phone: " in beginning of number.

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          I wish I could, but I have no idea what the data you are dealing with looks like. You would need to post a sample.

          Did you look at the code I posted above? That will find lines that start with "Phone:". If you want to match anything after it, you just add to it.

          As for the version of Perl, I have only coded in Perl 5 and have not started on Perl 6, so this should work fine for you.

          Regards,

          Jeff

          Comment

          • MrVon
            New Member
            • Nov 2009
            • 6

            #6
            It looks like web page HTML with phone on it saying "Phone: (123) 456-7890" somewhere on the page.

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              In your original thread start message, you said it was a text file, not an HTML file.

              If its an HTML file, its a different game and you will have to parse the HTML and get the value from the element that it is in.

              Otherwise, if it is in a text file, and in the format that you described, try something like the following:

              Code:
              #!/usr/bin/perl
              
              use strict;
              use warnings;
              
              my $string = "Phone: (123) 456-7890";
              
              if( $string =~ /^Phone:\s+(\(\d{3}\)\s*\d{3}-\d{4})/){
                  print("The phone number is: $1\n");
              }
              else{
                  print("Sorry, no number found.  Adjust your regex.\n");
              }
              Again, if its inside of html, you will have to look at modules to parse html.

              Regards,

              Jeff

              Comment

              • MrVon
                New Member
                • Nov 2009
                • 6

                #8
                I dont have perl experience, program i use allow writting scripts in perl 5, its just string field where you can enter code you want to use, thats what program documentation saying.

                Regular Expression: A regular expression is a formal description of a template to be matched against a text string. It is a pattern that is matched against a subject string from left to right, through using a sequence of escape characters to describe a set of characters with special meanings.

                A regular expression (sometimes abbreviated to "regex") is a way for a computer user or programmer to express how a computer program should look for a specified pattern in text and then what the program is to do when each pattern match is found. For example, a regular expression could tell a program to search for all text lines that contain the word "Windows XP" and then to print out each line in which a match is found or substitute another text sequence (for example, just "Windows") where any match occurs.

                Regular expressions are used for advanced context sensitive searches and text modifications.

                The syntax of regular expressions in InfoSeek is Perl 5 compatible. To distinguish from normal expressions, a regular expression defined in InfoSeek must contain a comment: (?#xxx) - xxx is the content of the comment and it may be empty.

                We advise common users to use normal expressions only because of the complexity and speciality of regular expressions.
                I need someone to edit code i posted in first message (that is actually works) to get not all phone numbers, but only ones with "Phone: " in front of it, can you suggest how this line of code should look like?

                Comment

                • chaarmann
                  Recognized Expert Contributor
                  • Nov 2007
                  • 785

                  #9
                  Read about "positive lookbehind" in regular expressions.

                  That means, simply add "(?<=Phone: )" in front of your regex-string.

                  As what I understand from the listing above is that you are using InfoSeek and have only a single input line for a pearl-like regex-string.
                  You should have told us that before.

                  Comment

                  • MrVon
                    New Member
                    • Nov 2009
                    • 6

                    #10
                    Please problem is still unsolved, i need complete string of regular expression to use.

                    Or can someone write REGEX that searches "Phone:" and then shows 14 characters that come after it as result, only thing it has to be one line.

                    Comment

                    • numberwhun
                      Recognized Expert Moderator Specialist
                      • May 2007
                      • 3467

                      #11
                      Ok, lets take a step back here.....

                      In post #6 you gave a sample of what you need to match. In post #7 I gave you some code that matches what you needed to match and captures the number itself. That is easily modified to do whatever you need to do.

                      I don't know what else you are looking for but considering you have admited in post #8 to having no Perl experience, that tells me that you need to pick up a tutorial and start learning because at this point, I don't think you actually know what you want.

                      If you know nothing of the language and its capabilities, how are you going to learn or know what you want to do?

                      I am not trying to be mean, just pointing out that you really need to learn some Perl so you can understand fully what you are being provided, especially since an answer in the form of code was given to you.

                      Regards,

                      Jeff

                      Comment

                      • chaarmann
                        Recognized Expert Contributor
                        • Nov 2007
                        • 785

                        #12
                        And what about my answer in post #9 ???
                        I wrote:
                        "...simply add "(?<=Phone: )" in front of your regex-string."
                        Have you tried it?

                        If you have a space after the word "Phone" before the phone number, than you can try with "(?<=Phone: )" instead. That means, your whole regex will be
                        Code:
                        (?<=Phone: )(?:(?:\d{1,3}[-. ]?)?[(]?\d{2,4}(?:[-). ]|[)][ ]))?\d{1,4}[-. ]?\d{4}(?:[-/.* ]\d{1,5})?
                        This is a simple copy-and-paste operation that you can do without any understanding of regular expressions or perl.

                        This is your complete solution. It should work.
                        If not then please tell me the effect (or error) it caused more detailed.
                        A "problem is still unresolved" as answer is not enough.

                        Comment

                        Working...