Matching substrings within a Regex

Discussion in 'Perl Misc' started by newjazzharmony, May 16, 2006.

  1. Hi,

    I need some help crafting a regular expression.
    Consider the following literal expressions:

    Literal String A:
    AccountNumber="

    Literal String B:
    StreetNumber="

    I want to find all instances where String A is followed by string B
    (with any number of miscellaneous characters in between).

    The catch is that I want to capture only instances where the left four
    digits of the AccountNumber equals the left four digits of the
    StreetNumber.

    Examples:

    I want to capture the following strings:

    AccountNumber="11324324" blablablablabla StreetNumber="11322345"
    AccountNumber="234534" blabla StreetNumber="2345999554"

    I DO NOT want to capture the following string:

    AccountNumber="345635" blabla StreetNumber="6789689"


    Thanks!
    Jonathan
     
    newjazzharmony, May 16, 2006
    #1
    1. Advertisements

  2. use strict; use warnings;

    while ( <DATA> ) {
    /AccountNumber="(\d{4}).*StreetNumber="\1/ and print "matched.\n";
    }

    __DATA__
    AccountNumber="11324324" blablablablabla StreetNumber="1322345"
     
    it_says_BALLS_on_your forehead, May 16, 2006
    #2
    1. Advertisements

  3. newjazzharmony

    David Squire Guest

    What have you tried so far?
    You need to learn about backreferences in regexes. See perldoc perlre.

    For example: /([A-Z]).\1/ matches AaA and B2B, but not AaB or B2A.

    DS
     
    David Squire, May 16, 2006
    #3

  4. sorry, this illustrates the OP's request more fully.

    use strict; use warnings;

    while ( <DATA> ) {
    /AccountNumber="(\d{4}).*StreetNumber="\1/ and print "matched.\n" or
    print "did not match.\n";
    }

    __DATA__
    AccountNumber="11324324" blablablablabla StreetNumber="1322345"
    AccountNumber="11324324" blablablablabla StreetNumber="11322345"

    __OUTPUT__
    did not match.
    matched.
     
    it_says_BALLS_on_your forehead, May 16, 2006
    #4
  5. Thanks for the replies.
    I should have specified that I am not actually using PERL...I just
    figured this would be the best place to get expert Regex advice :)

    I was hoping there would be a way to do this without any programmatic
    structures (e.g., the while loop in the example).
    Is there a way to codify all these rules (including the AcctNum to
    StreetNum match) in the regex string itself?

    If possible, I would like to do this in a simple text editor that
    supports regular expressions.

    Thanks,
    Jonathan
     
    newjazzharmony, May 16, 2006
    #5
  6. Thanks for the replies.
    I should have specified that I am not actually using PERL...I just
    figured this would be the best place to get expert Regex advice :)

    I was hoping there would be a way to do this without any programmatic
    structures (e.g., the while loop in the example).
    Is there a way to codify all these rules (including the AcctNum to
    StreetNum match) in the regex string itself?

    If possible, I would like to do this in a simple text editor that
    supports regular expressions.

    Thanks,
    Jonathan
     
    newjazzharmony, May 16, 2006
    #6
  7. **PLONK**

    Sinan
     
    A. Sinan Unur, May 16, 2006
    #7
  8. newjazzharmony

    DJ Stunks Guest

    Agreed.

    Is today Internet Jackass Day? I must have missed a memo because they
    seem to be coming out of the woodwork...

    -jp
     
    DJ Stunks, May 16, 2006
    #8
  9. Is today Internet Jackass Day? I must have missed a memo because they
    I'll gladly take the internet jackass lebel if you can answer my
    question :)
     
    newjazzharmony, May 16, 2006
    #9
  10. newjazzharmony

    David Squire Guest

    You don't know what *plonk* means do you? See
    http://en.wikipedia.org/wiki/Plonk

    .... and if you intend to continue to post here in the hope that some are
    still seeing you, read the posting guidelines for this group first and
    follow them - starting with properly attributing quoted context.

    DS
     
    David Squire, May 16, 2006
    #10
  11. You don't know what *plonk* means do you?

    Plonk, as a disparaging term for cheap wine, especially cheap red wine,
    is now widely known in the UK and also to a lesser extent in the USA.
    It's so fixed a part of British English that many people are
    surprised to hear that it's originally Australian.
     
    newjazzharmony, May 16, 2006
    #11
  12. newjazzharmony

    David Squire Guest

    OK. That was your last chance from me.

    *plonk*
     
    David Squire, May 16, 2006
    #12

  13. Whose replies?

    Please quote some context in followups like everybody else does.


    Making off-topic postings is a quick way to wear out your welcome.


    And you have received the expert Regex advice that you sought.


    The while loop is only for reading the data.

    It is not germane to the regex that you asked for.


    Yes. And you have already been given that.

    So what is the problem again?


    Then you already have everything that you need.

    Use the regex that you've been given.

    If Perl regexes don't work in your "simple text editor" then
    your question is off-topic here in a Perl newsgroup.
     
    Tad McClellan, May 16, 2006
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.