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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a regular expression 1

Status
Not open for further replies.

pkailas

Programmer
Jun 10, 2002
555
US
I'm using VB and I'm trying to match certain criteria using regex.

I want a match on "payment terms" or "terms" but NOT on "freight terms".

Everything I've done picks up "freight terms" if I allow "terms".

Please help.

Thanks

Paul

_______
I love small animals, especially with a good brown gravy....
 
Here's a great resource for regular expressions:
Regular Expressions Library:
What's happening in your case is that the RegEx is only matching 'terms' and not the entire clause. To get arround this precede your RegEx with ^ and end it with $. The carat specifies the beginning of a line, and the $ specifies the end of a line. This forces your the match to go against the entire phrase, and not just pick out 'terms'.

So, your regex should look like this:
Code:
"^(terms)|(payment terms)$

Take Care,
Mike
 
I can force the complete phrase no problem. the thing is that "terms" by itself is allowed, but it CANNOT be preceded by "freight", but it CAN be preceded by "payment"

_______
I love small animals, especially with a good brown gravy....
 
Yes that does work. Sorry I didn't test it first.

Is there a way to make it case insensitive as well?

_______
I love small animals, especially with a good brown gravy....
 
If you use PERL style RegEx (that include a beginning and ending /) then do this:
Code:
/^(terms)|(payment terms)$/i
.

If that doesn't work in your situation, then make sure the variable you are testing has been converted to lowercase.

Take Care,
Mike
 
well, I'm using a crappy application that has regex built in and it doesn't seem to support that. I'll just have to enter all possible iterations.

One more question. How to I tell this to ignore spaces?

ie. " Terms" causes a match

_______
I love small animals, especially with a good brown gravy....
 
Try this. I added a little shortcur for ignoring capital first letters, as well as the optional space in front:
Code:
^( )?([tT]erms)|([pP]ayment [tT]erms)$

Take Care,
Mike
 
almost there!

Only thing, when I tell it to ignore the space, then "Fright Terms" causes a match.

is there a way to "trim" the line before we test it?

This has to be done as a regex expression though

_______
I love small animals, especially with a good brown gravy....
 
I'm not quite sure how you are implementing this. Can you post the code you are using to evaluate the RegEx?

Take Care,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top