Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a doubt on how to do a regex match only with alphanumeric. I am putting a scenario here pls help.

$data_main = "The ideal Rack cleaning support candidate is dedicated t +o providing outstanding customer service every day. New Store Opening Promenade in Crocker Park Rack Nordstrom Rack is a division of Nordstrom, a fashion specialty retaile +r founded on a simple idea: offer customers the best possible service +, quality, value and selection. We are looking for motivated people t +o provide cleaning services to maintain a pleasant shopping environme +nt for our customers. Nordstrom Rack is where customers go for top-of-the-line Nordstrom fas +hion and accessories at fabulous values. When you work for Nordstrom +Rack, you'll enjoy flexible hours, a friendly atmosphere and competit +ive pay and benefits. You'll also be a part of a team that is committ +ed to supporting your career goals. We are always looking for exceptional people who share our love for th +e customers we serve, the merchandise we sell and the work we do!"; $out = $1 if ($data_main =~ /(NewStoreOpeningPromenadeinCrockerParkRac +kNordstromRackisadivisionofNordstrom.*)/is); print $out; exit; Desired Output -------------- New Store Opening Promenade in Crocker Park Rack Nordstrom Rack is a division of Nordstrom, a fashion specialty retaile +r founded on a simple idea: offer customers the best possible service +, quality, value and selection. We are looking for motivated people t +o provide cleaning services to maintain a pleasant shopping environme +nt for our customers. Nordstrom Rack is where customers go for top-of-the-line Nordstrom fas +hion and accessories at fabulous values. When you work for Nordstrom +Rack, you'll enjoy flexible hours, a friendly atmosphere and competit +ive pay and benefits. You'll also be a part of a team that is committ +ed to supporting your career goals. We are always looking for exceptional people who share our love for th +e customers we serve, the merchandise we sell and the work we do!

Basically, I only want to include the alphanumeric only to do the matching process, NO SPACES, NO NEW LINES etc.. How can I achieve it?

Thanks for your time.

Replies are listed 'Best First'.
Re: Perl Regex Match (Compare Only AlphaNumeric)
by choroba (Cardinal) on Aug 23, 2013 at 11:05 UTC
    Please, try to rephrase your goal. I do not understand what you are trying to achieve, and your code does not help me much either.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Please consider this scenario. I have a string which have its value with only alphanumerics & No spaces or new lines. So I want to take this and match with another string which has a value which includes spaces, newlines etc.... So I want to match that alphanumeric and take only the desired output.
Re: Perl Regex Match (Compare Only AlphaNumeric)
by daxim (Curate) on Aug 23, 2013 at 11:09 UTC
    The difference between the input and the desired output is the first two lines of the input. Simply remove them with the substitution operator:

    $data_main =~ s {\AThe ideal Rack cleaning support candidate is dedicated to provi +ding outstanding customer service every day[.]\n\n} {}ms;

    If that's not what you want, you have to explain your problem more clearly.

Re: Perl Regex Match (Compare Only AlphaNumeric)
by AnomalousMonk (Archbishop) on Aug 23, 2013 at 11:55 UTC
    $out = $1 if ($data_main =~ /(NewStoreOpeningPromenadeinCrockerParkRackNordstromRackisadivisionofNordstrom.*)/is);

    There is no "ignore non-alphanumeric characters when matching" regex modifier available in the standard Perl regex engine. (It's apparently possible to create and use your own regex engine in later versions of Perl 5 and in Perl 6, but I've never done or even attempted anything like this.) You must either take whitespace, etc., characters into account when matching, or else remove all such characters before comparing, matching, etc., both of which approaches have been suggested or exemplified by others. Please see perlre, perlretut, perlrequick.

      okay, I will cehck those links Anamolous Monk. Thank You.
Re: Perl Regex Match (Compare Only AlphaNumeric)
by hdb (Monsignor) on Aug 23, 2013 at 11:09 UTC

    Remove all the uninteresting characters from your data before testing for match, e.g. s/[^a-zA-Z0-9]//g.

      hdb, but how can I make it back as it was...? if I remove the uninteresting characters, the there will be No space or new line in the desired output.

        Work on a copy of the data for the testing...

        UPDATE: I think I now understand what you want. Try something like this:

        my $regex = join '\s+', qw( New Store Opening Promenade in Crocker Par +k Rack Nordstrom Rack is a division of Nordstrom ); $out = $1 if ($data_main =~ /($regex.*)/is);

        If you're not interested in them, why put them back? As choroba suggested, a clearer statement of the problem may lead to better understanding for all, including yourself.

Re: Perl Regex Match (Compare Only AlphaNumeric)
by McA (Priest) on Aug 23, 2013 at 11:36 UTC

    Besides any regex problem. The whole thing let me have an idea how to place advertising in perlmonks without the people letting know that's advertising (not saying that this is the case here).

    Like: Hey Monks, I have this big text (full of marketing blabla). And I want to have the following big text (full of marketing blabla). How do I achieve this without regex, but with map, if-statements shall not be in there. And by the way: Pragmas are not my thing.

    ;)

    McA

      without the people letting know that's advertising Then what is the use???
        They call it a "Product placement".
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Perl Regex Match (Compare Only AlphaNumeric)
by Anonymous Monk on Aug 23, 2013 at 11:23 UTC