How to grep hyphen ('-') in a string?

W

William Tang

I have a string such as:
XYZ-ABC.xml NEW 1.0 myfolder/mylocation wtang.001

I use the following grep function try to grep it:
grep(/^\s*\w+\.\w+\s+([0-9\.]+|New)\s+[0-9\.]+\s+[\w\\\/\.]+\s+\S+\s*$/,
<SCRFILE> );

where <SCRFILE> is the filename that store this string.

It seems that it can't read this '-'. If I change it into '_', it
works fine.

Did I missing something?

Thanks,
William
 
G

Guru03

(e-mail address removed) (William Tang) wrote in
I use the following grep function try to grep it:
grep(/^\s*\w+\.\w+\s+([0-9\.]+|New)\s+[0-9\.]+\s+[\w\\\/\.]+\s+\S+\s*$/,
<SCRFILE> );

where <SCRFILE> is the filename that store this string.

It seems that it can't read this '-'. If I change it into '_', it
works fine.

Did I missing something?

use this:

grep(/^\s*[\w\-]+\.\w+
 
P

Paul Lalli

I have a string such as:
XYZ-ABC.xml NEW 1.0 myfolder/mylocation wtang.001

I use the following grep function try to grep it:
grep(/^\s*\w+\.\w+\s+([0-9\.]+|New)\s+[0-9\.]+\s+[\w\\\/\.]+\s+\S+\s*$/,
<SCRFILE> );

where <SCRFILE> is the filename that store this string.

It seems that it can't read this '-'. If I change it into '_', it
works fine.

Did I missing something?


Yes. You missed the fact that \w does not match a hyphen. \w matches
letters, numbers, and the underscore.

Paul Lalli
 

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

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,375
Latest member
FelishaCma

Latest Threads

Top