Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression 1

Status
Not open for further replies.

bitwise

Programmer
Mar 15, 2001
269
0
0
US
For example, given the sentence:

"... interest rates discouraged further buying interests_1 ..."

I need to match "interests_1" only.

If I use the regular expression:

/interest.*_[1-9]/

It will match the entire sentence because of the "interest" prior to "interests_1". How can I modify my reg. exp. to account for this? Do I need to use word boundaries somehow?

Thanks,
-bitwise
 
You could use [tt]/interest\S*_[1-9]/[/tt]

The [tt]\S*[/tt] would match any number of non space charactes, which would keep it from getting the first "interest".

It's hard to know if this is exactly what you're looking for, though.
 
If I understand your question correctly then I'm almost certain that the following will work.

/interests_[1-9]/

There is no need for the .* but as I said I may have misunderstood your question..

Hope I've helped,

Ryan
 
If you are trying to match 'interest_1' and/or 'interests_1' then you can narrow down the regex to
Code:
/interests?_[1-9]/
which just makes the last 's' optional.

jaa
 
I'm sorry, I thought I mentioned this but the regular expression needs to match words like:

interest_1
interests_1
interested_1
interesting_1

That is why the .* is in my regular expression, however, it doesn't work completely b/c it matches "interest...etc". I need to make it say that it needs the underscore and the number as well.

Thanks,
-bitwise

 
rosenk's solution can be adapted as long as you know exactly what you are prepared to allow:

[tt]/interest\w*_\d/[/tt]
allows "word" characters (alphanumeric plus "_")

[tt]/interest[-abcdfhl-r]*_\d/[/tt]
allows only the listed characters (hyphen, if required, must be first as ranges like [tt]l-r[/tt] are legal).

I suspect that
[tt]/interest[-a-z]*_\d/[/tt]
is what you are really after, to allow interest-earning_1 but not interest1_1.

Yours,

fish "As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top