Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
$_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ;

First thing: if you don't supply a variable, the regex will automatically work on $_, so you shouldn't include that. It's confusing and may lead to bugs. Just put the /regex/.

Second, your regex doesn't actually do anything. It checks the pattern against your chomped line, but it doesn't do anything to it. To manipulate it, you'd need to add another section and make it a s/// operator, with the replacement portion between the second and third delimiters. I don't know what you do want to do to it, so for now, here's what your pattern is checking for. I'm going to use the /x modifier so that I can include whitespace and comments, which I'd suggest you do in the future unless you're dealing with a simple enough regex that it's obvious at a glance what it does.

/^ # anchored to the beginning of the string \w+ # one or more word characters (a word) \s* # zero or more whitespace characters = # an equals sign \s* # zero or more whitespace characters \w+ # one or more word characters (a word) \s* # zero or more whitespace characters \, # a comma (you actually don't need to backslash this) \? # a question mark \w+ # one or more word characters (a word) /x;

As you can see now, this will require that a matching line include a comma followed by a question mark. That's probably not what you want. It also requires a third "word" following the question mark. A good method would be to write out exactly what you want to match, as I've done with my comments above, and then figure out the regex pattern piece by piece from there. Then explain what changes you want to make to it, and we can figure out the second part.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.


In reply to Re^9: Need advice on checking two hashes values and keys by aaron_baugher
in thread Need advice on checking two hashes values and keys by perlynewby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found