regular expression help with apostrophe

Discussion in 'Perl Misc' started by dmalhotr2001@yahoo.com, Mar 15, 2005.

  1. Guest

    Hi,

    I would like to figure out a certain issue.

    I have a regular expression [^\']+ that checks whether there's a
    apostrophe in a particular string. In my case I'm using a file path
    and filename so for example.

    C:\Documents and Settings\test_user\Local Settings\Temporary Internet
    Files\Content.IE5\testing.html

    Now this works to check if there is an apostrophe.

    However I don't know how to check ONLY if there is an apostrophe in the
    file name, meaning if there is a apostrophe in testing.html example
    above. I would want it to ignore it if Local's Settings folder has an
    apostrophe.

    Thus, what regular expression would yield an apostrophe just in the
    filename at the end and would ignore apostrophes in the folder paths.

    Thanks in advance.

    :D
     
    , Mar 15, 2005
    #1
    1. Advertising

  2. Bob Walton Guest

    wrote:

    ....
    > I have a regular expression [^\']+ that checks whether there's a
    > apostrophe in a particular string. In my case I'm using a file path
    > and filename so for example.
    >
    > C:\Documents and Settings\test_user\Local Settings\Temporary Internet
    > Files\Content.IE5\testing.html
    >
    > Now this works to check if there is an apostrophe.
    >
    > However I don't know how to check ONLY if there is an apostrophe in the
    > file name, meaning if there is a apostrophe in testing.html example
    > above. I would want it to ignore it if Local's Settings folder has an
    > apostrophe.
    >
    > Thus, what regular expression would yield an apostrophe just in the
    > filename at the end and would ignore apostrophes in the folder paths.


    Well, it isn't too clear what you want. I have assumed you want
    a regexp that will match if a Windoze path and file name has no
    apostrophe in the filename portion, and will fail to match if the
    filename portion of the path and file name contains one or more
    apostrophes. If that is correct, try:

    use warnings;
    use strict;
    while(<DATA>){
    chomp;
    /(?:.*\\|^)[^']+$/
    ?
    print "$_ has no apostrophe in filename\n"
    :
    print "$_ has at least one apostrophe in filename\n";
    }
    __END__
    c:\no apostrophe\here.ext
    c:\one ' apostrophe\here.ext
    c:\file\has\apost'rophe.ext
    justa.file
    c:\justa.file
    c:\just'a.f'''''ile
    c:'\justa.file
    path\file'''''.ext
    just a path\

    ....

    > :D

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Mar 15, 2005
    #2
    1. Advertising

  3. <> wrote:

    > I have a regular expression [^\']+ that checks whether there's a
    > apostrophe in a particular string.



    Single quotes are not special, there is no need to backslash them.


    > I would want it to ignore it if Local's Settings folder has an
    > apostrophe.



    What is a Local's Settings folder?


    > Thus, what regular expression would yield an apostrophe



    None.

    Regular expressions do not "yield" characters.

    They either match (succeed), or they don't match (fail).


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 15, 2005
    #3
  4. In article <>,
    Tad McClellan <> wrote:
    : <> wrote:

    :> I have a regular expression [^\']+ that checks whether there's a
    :> apostrophe in a particular string.

    :Single quotes are not special, there is no need to backslash them.

    There is if you are using perl -e '...' or if you are constructing
    a single-quoted string that will be extrapolated into a pattern.

    The first of these is, of course, an artifact of shell quoting
    conventions; the second is perl itself. You can use the
    q{} or qw{} operators though... which changes the delimeter
    but does not evade the fact that there will be -some- character
    you have to escape if it appears in the string.
    --
    Entropy is the logarithm of probability -- Boltzmann
     
    Walter Roberson, Mar 15, 2005
    #4
  5. * Bob Walton wrote:

    > Well, it isn't too clear what you want. I have assumed you want
    > a regexp that will match if a Windoze path and file name has no
    > apostrophe in the filename portion, and will fail to match if the
    > filename portion of the path and file name contains one or more
    > apostrophes. If that is correct, try:
    >
    > use warnings;
    > use strict;
    > while(<DATA>){
    > chomp;
    > /(?:.*\\|^)[^']+$/
    > ?
    > print "$_ has no apostrophe in filename\n"
    > :
    > print "$_ has at least one apostrophe in filename\n";
    > }
    > __END__
    > just a path\


    If your string could be "just'a'path\" then your code will say there is
    "at least one apostrophe in filename" -- but it's just a path without a
    filename ;-)

    Hence, I'd change the latter »+« in your regex to »*«, so that it could
    match on empty filenames too.

    regards,
    fabian
     
    Fabian Pilkowski, Mar 15, 2005
    #5
  6. Peter Wyzl Guest

    "Tad McClellan" <> wrote in message
    news:...
    > <> wrote:
    >
    > > I have a regular expression [^\']+ that checks whether there's a
    > > apostrophe in a particular string.

    >
    >
    > Single quotes are not special, there is no need to backslash them.
    >
    >
    > > I would want it to ignore it if Local's Settings folder has an
    > > apostrophe.

    >
    >
    > What is a Local's Settings folder?


    That is like asking "What is a /usr/var directory?"

    Whilst not a direct equivalent, it is a common enough environment to be
    accepted as known by any competent windows user.

    Never mind. I think the question is just as valid without the specific
    example,

    "How do I tell if the filename part of the string has a single quote, and
    ignore the rest of the pathname?"

    ....

    use File::Basename;

    is a good start...

    P
     
    Peter Wyzl, Mar 15, 2005
    #6
  7. phaylon Guest

    Peter Wyzl wrote:

    > That is like asking "What is a /usr/var directory?"


    Wouldn't say "/usr/var" is a "fully qualified" pathname. "Local Settings
    Folder" is not.

    > Whilst not a direct equivalent, it is a common enough environment to be
    > accepted as known by any competent windows user.


    Some people haven't seen Windows for a long time :D

    --
    http://www.dunkelheit.at/
    ...and in darkness bind them.
     
    phaylon, Mar 15, 2005
    #7
  8. Peter Wyzl <> wrote:
    > "Tad McClellan" <> wrote in message
    > news:...
    >> <> wrote:



    >> > I would want it to ignore it if Local's Settings folder has an
    >> > apostrophe.

    >>
    >>
    >> What is a Local's Settings folder?

    >
    > That is like asking "What is a /usr/var directory?"
    >
    > Whilst not a direct equivalent, it is a common enough environment to be
    > accepted as known by any competent windows user.



    But I am an incompetent windows user, so I though I might
    learn some(off-topic)thing:

    Does windows have a way to specify the characters allowed in pathnames?

    That's what it sounded like to me...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 15, 2005
    #8
  9. Guest

    Jesus christ,

    You guys are overthinking this to death. keep the thing simple

    c:\testing's folder\filename.ext

    I want a regular expression to ignore the above because - testing's
    folder is a directory.

    But if its:

    C:\testing folder\what's_up.ext

    I want the regular expression to yield.

    Any questions??
     
    , Mar 15, 2005
    #9
  10. wrote in news:1110906507.977082.212230
    @g14g2000cwa.googlegroups.com:

    > Jesus christ,
    >
    >
    > You guys are overthinking this to death.


    And you are in a position to judge how?

    > keep the thing simple
    >
    > c:\testing's folder\filename.ext
    >
    > I want a regular expression to ignore the above because - testing's
    > folder is a directory.


    Here is what you originally stated:

    <blockquote>
    I have a regular expression [^\']+ that checks whether there's a
    apostrophe in a particular string. In my case I'm using a file path
    and filename so for example.
    </blockquote>

    The requirement you set out in this message does not match the original
    requirement.

    In any case, Peter pointed you to File::Basename. Have you not read the
    documentation for that module yet.

    Using that module, split the string into the various components. You
    will be interested in one of those components I presume. Then, check if
    that string contains an apostrophe.

    If you are only interested in the existence of a single character in a
    string, you should also look at:

    perldoc -f index

    > I want the regular expression to yield.
    >
    > Any questions??


    *PLONK*
     
    A. Sinan Unur, Mar 15, 2005
    #10
  11. Guest

    Thanks, thats exactly what i needed.

    :D
     
    , Mar 15, 2005
    #11
  12. Ted Zlatanov Guest

    On Mon, 14 Mar 2005, wrote:

    > Regular expressions do not "yield" characters.
    >
    > They either match (succeed), or they don't match (fail).


    This is not always true, e.g.

    perl -n -e '@all = m/(.)/g; print @all'

    to give a simple example of a regex that returns characters. Maybe I
    misunderstood your intent, Tad.

    Ted
     
    Ted Zlatanov, Mar 15, 2005
    #12
  13. * Hendrik Maryns wrote:
    > > >
    > > > Regular expressions do not "yield" characters.
    > > >
    > > > They either match (succeed), or they don't match (fail).

    > >
    > > This is not always true, e.g.
    > >
    > > perl -n -e '@all = m/(.)/g; print @all'

    >
    > This does nothing at all here. Actually, it just blocks. (And that is
    > after changing the ' to " to work on my Windoze machine.)
    >
    > What should it do?


    Have you read `perldoc perlvar` to learn more about the »-n« switch? It
    adds a while loop with the diamond operator around the code which makes
    it iterate over filename arguments. Since there are no arguments in the
    line above the script will block and wait for input. On Windoze machines
    the script will start its work if one hits some keys followed by enter.
    To see the effect a bit better, I'd change the line to

    perl -ne "@all = m/(.)/g; print qq{@all\n}"

    Btw, by hitting Ctrl-Z as input your script terminates regularly ;)

    regards,
    fabian
     
    Fabian Pilkowski, Mar 15, 2005
    #13
  14. <> wrote:

    > Jesus christ,



    I am pretty sure that he does not subscribe to this newsgroup.


    > You guys are overthinking this to death.



    That is to compensate for your underthinking when composing
    your initial question.

    You can expect that the value of followups will be directly
    proportional to the amount of thought you put into composing
    your question.

    A silly question that nobody can figure out is likely to yield
    a bunch of silly followups asking questions in an attempt to
    figure it out (or worse).


    > I want a regular expression to ignore the above



    Regular expressions don't "ignore" they either succeed/match
    or fail/don't match.

    It is still not clear to me whether you want the match to succeed
    or to fail above...


    > I want the regular expression to yield.



    Want the regular expression to yield _what_?

    A true value if it matches or a false value if it doesn't match?

    It will already do that.


    > Any questions??



    Yes, see above.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 15, 2005
    #14
  15. Ted Zlatanov <> wrote:
    > On Mon, 14 Mar 2005, wrote:
    >
    >> Regular expressions do not "yield" characters.
    >>
    >> They either match (succeed), or they don't match (fail).

    >
    > This is not always true, e.g.
    >
    > perl -n -e '@all = m/(.)/g; print @all'
    >
    > to give a simple example of a regex that returns characters. Maybe I
    > misunderstood your intent, Tad.



    Yeah, I should have said:

    Pattern matches in scalar context to not "yield" characters...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 15, 2005
    #15
  16. <> wrote:

    > Thanks, thats exactly what i needed.



    Which that is that that?

    It is customary to quote some context in followups.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 15, 2005
    #16
  17. In article <>,
    Tad McClellan <> wrote:
    : <> wrote:

    :> Jesus christ,

    :I am pretty sure that he does not subscribe to this newsgroup.

    "Retro me, perlus!" ?
    --
    'The short version of what Walter said is "You have asked a question
    which has no useful answer, please reconsider the nature of the
    problem you wish to solve".' -- Tony Mantler
     
    Walter Roberson, Mar 15, 2005
    #17
  18. Bob Walton Guest

    Fabian Pilkowski wrote:

    > * Bob Walton wrote:
    >

    ....
    >> /(?:.*\\|^)[^']+$/

    ....
    >>__END__
    >>just a path\

    >
    >
    > If your string could be "just'a'path\" then your code will say there is
    > "at least one apostrophe in filename" -- but it's just a path without a
    > filename ;-)
    >
    > Hence, I'd change the latter »+« in your regex to »*«, so that it could
    > match on empty filenames too.

    ....
    > fabian


    Good catch. Thanks.

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Mar 15, 2005
    #18
  19. Peter Wyzl Guest

    "phaylon" <> wrote in message
    news:p...
    : Peter Wyzl wrote:
    :
    : > That is like asking "What is a /usr/var directory?"
    :
    : Wouldn't say "/usr/var" is a "fully qualified" pathname. "Local Settings
    : Folder" is not.

    What I was getting at is that 'Local Settings Folder' is a well known and
    expected default storage location on a Windows system. Likewise /usr/var is
    a well known and expected default storage location on a *nix system.

    There are many differences, not least being the semantic one you pointed
    out, but that is not what I was getting at.

    P
     
    Peter Wyzl, Mar 16, 2005
    #19
  20. Ted Zlatanov Guest

    On Tue, 15 Mar 2005, wrote:

    > Ok, this works now, but I still don't see the whole point:
    > <quote>
    >>Regular expressions do not "yield" characters.
    >>>
    >>> They either match (succeed), or they don't match (fail).

    >
    >
    > This is not always true, e.g.
    >
    > perl -n -e '@all = m/(.)/g; print @all'
    >
    > to give a simple example of a regex that returns characters.
    > </quote>
    >
    > This isn't a regex that returns characters, it are the extended Perl
    > memory functions concerning regexes that make this possible (i.e. (.)
    > returning a value, $1), but a regex 'an sich' indeed only matches or
    > doesn't. (One could also see a regex as a description of a certain set
    > of strings).


    We are discussing Perl, so yes, *Perl* regexes return values. It's
    not something external to them, it's a feature of the capturing
    mechanism in the regex engine. You may be thinking of the computer
    science definition of regexes as FSMs - Perl goes far beyond that.

    This example does not have anything to do with the $1...$9 variables.
    There are only 9 of them, while the example above will match and
    return any number of characters into @all.

    I have used this feature in real code many times. For example, to
    extract data from a format like this:

    $text = "Field1 = Value1
    Field2 = Value2
    Field3 =
    Field4 = Value4";

    you can use a regex like this:

    my %results = ($text =~ m/^(.*?)\s*=\s*?(.*?)$/gm);

    and this populates %results with the right key/value pairs, no matter
    how many there are.

    Hope that helps...
    Ted
     
    Ted Zlatanov, Mar 16, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,336
  2. paul reed
    Replies:
    0
    Views:
    433
    paul reed
    Oct 17, 2003
  3. Replies:
    3
    Views:
    3,390
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    863
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,998
    vforvikash
    Dec 29, 2008
Loading...

Share This Page