regular expression help with apostrophe

D

dmalhotr2001

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
 
B

Bob Walton

(e-mail address removed) 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\

....
 
T

Tad McClellan

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).
 
W

Walter Roberson

:> 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.
 
F

Fabian Pilkowski

* Bob Walton said:
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
 
P

Peter Wyzl

Tad McClellan said:
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
 
P

phaylon

Peter said:
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
 
T

Tad McClellan

Peter Wyzl said:
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...
 
D

dmalhotr2001

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??
 
A

A. Sinan Unur

(e-mail address removed) wrote in @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*
 
T

Ted Zlatanov

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
 
F

Fabian Pilkowski

* Hendrik Maryns said:
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
 
T

Tad McClellan

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.
 
T

Tad McClellan

Ted Zlatanov said:
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...
 
W

Walter Roberson

:> Jesus christ,

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

"Retro me, perlus!" ?
 
B

Bob Walton

Fabian said:
* 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.
 
P

Peter Wyzl

: 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
 
T

Ted Zlatanov

Ok, this works now, but I still don't see the whole point:



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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top