Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Regular Expression on special character and alphanumeric values

by perlmad (Sexton)
on May 30, 2016 at 16:34 UTC ( [id://1164517]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks

I have a doubt on regular expression below is my code

my $string="{'totalResultsCount':71-24,'securityList':[{'cusip':'91%27 +9-6.H:Y8','issueDate':'2016-06-02T00:00:00','securityType':'Bill'}"; $string =~ /'cusip':'(\w+\-\$\.\%)'/; print " first is : $1 \n\n";

output is

Use of uninitialized value in concatenation (.) or string at sample.pl + line 7. first is :

Because the "\w+" on supports alphanumeric values but my question is how to include alphanumeric and special character with in the parenthesis , I have tried the above code but i could not get an exact answer , Is there need any changes in Regular expression ???

Expected output

first is :912796HY8

Replies are listed 'Best First'.
Re: Regular Expression on special character and alphanumeric values
by Eily (Monsignor) on May 30, 2016 at 17:01 UTC

    It seems like you don't need to define exactly what characters can go in your value. You just want what's inside the single quotes after 'cusip'. '(.*?)' means the shortest possible string between two quotes (so the text 'ab' cd' would yield "ab" and not "ab' cd").

    But it looks like you are trying to parse JSON. If you need to get more than one value from your string, decoding the serialized data into a perl structure may be more helpful. JSON is pretty easy to use.

    Edit: Corion pointed out that the 71-24 doesn't look like serialized data (or at least not JSON), so while this could still be valid javascript or python code, JSON may get confused.

Re: Regular Expression on special character and alphanumeric values
by Corion (Patriarch) on May 30, 2016 at 16:46 UTC

    Maybe just read perlre on how to construct character classes other than \w.

    Also, $1 will only have a value if the match was successful, so the correct idiom to use is:

    if( $string =~ /.../ ) { print " first is : $1\n\n"; } else { print "Nothing found in <<$string>>\n"; };
Re: Regular Expression on special character and alphanumeric values
by johngg (Canon) on May 30, 2016 at 17:52 UTC

    You can construct an extended character class inside square brackets but make sure the hyphen is at the beginning or end of the character class or it will be interpreted as denoting a range, e.g [A-Za-z]. An alternative to Eily's non-greedy match is a negated character class, i.e. match anything that isn't a single quote.

    use strict; use warnings; use feature qw{ say }; my $string = "{'totalResultsCount':71-24,'securityList':[{'cusip':'91% +279-6.H:Y8','issueDate':'2016-06-02T00:00:00','securityType':'Bill'}" +; say $1 if $string =~ m{'cusip':'([\w:%.-]+)'}; say $1 if $string =~ m{'cusip':'([^']+)};

    Produces

    91%279-6.H:Y8 91%279-6.H:Y8

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Regular Expression on special character and alphanumeric values
by Marshall (Canon) on May 31, 2016 at 06:38 UTC
    I think the actual regex question has been dealt with. Here a couple of additional tips that I find useful when dealing with regex:

    1) Assign directly to a variable without using $1. The list on the left can be expanded, especially useful when there is $2,$3, etc. I prefer to get a meaningful name ASAP.

    my ($first) = string =~ /some regex with capture/;

    2) Often the //= operator is useful:

    $first //= '';
    Assigns $first to the null string if it is undefined. Introduced I think in Perl 5.10. You have something that evaluates to false, yet can be printed without an error.
Re: Regular Expression on special character and alphanumeric values
by $h4X4_&#124;=73}{ (Monk) on May 30, 2016 at 17:59 UTC

    You are trying to find '\w+\-\$\.\%' pattern with (\w+\-\$\.\%) group and it does not exist so $1 is undefined and u get an error. Try this.

    my $string="{'totalResultsCount':71-24,'securityList':[{'cusip':'91%27 +9-6.H:Y8','issueDate':'2016-06-02T00:00:00','securityType':'Bill'}"; # Patterns # original (\w+\-\$\.\%) # matach any pattern with these limits ([\w\-\$\.\%\:]+) # anything but this ' ([^\']+) # exact pattern (\d{2}\%\d{3}\-\d\.[A-Z]\:[A-Z]\d) $string =~ /'cusip':'(\d{2}\%\d{3}\-\d\.[A-Z]\:[A-Z]\d)'/; if (defined $1) { print " first is : $1 \n\n"; } else { print " first is not defined\n\n"; }

    Updated: added some regex in code.

Re: Regular Expression on special character and alphanumeric values
by james28909 (Deacon) on May 30, 2016 at 19:15 UTC
    my $string = "{'totalResultsCount':71-24,'securityList':[{'cusip':'91% +279-6.H:Y8','issueDate':'2016-06-02T00:00:00','securityType':'Bill'}" +; $string =~ /'cusip':'([\w\-\$\.\%\:]+)'/; if ( defined $1 ) { $string = $1; print "first is : $string \n" for $string =~ s/\W//g;; } else { print " first is not defined\n"; }
    C:\Users\james\Desktop\perlmonks>pm.pl first is : 912796HY8

    EDIT:

    shorter code:

    my $string = "{'totalResultsCount':71-24,'securityList':[{'cusip':'91% +279-6.H:Y8','issueDate':'2016-06-02T00:00:00','securityType':'Bill'}" +; $string =~ s/.*('cusip':'([\w\-\$\.\%\:]+)').*/$2/g; print "first is : $string \n" for $string =~ s/\W+//g;
    C:\Users\james\Desktop\perlmonks>pm.pl first is : 912796HY8

    EDIT: changed print "first is : $string \n" if $string =~ s/\W+//g; to print "first is : $string \n" for $string =~ s/\W+//g;

      thanks to all it's working awesome once again thanks for your precious time to spend to my question

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1164517]
Approved by Eily
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-18 00:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found