Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 13th, 2006, 7:01 AM   #1
ktsirig
Programmer
 
Join Date: Oct 2005
Posts: 54
Rep Power: 4 ktsirig is on a distinguished road
perl tricky pattern matching

Hi all!
Suppose you have the following string of letters :

$string ='LPSTEOPRTRYERTRETR';
and you want to search for the following pattern inside the string:
$pattern= LP[SKTAQEHLDN][TA][GN][EDASTV] =>5/6
By saying 5/6, I mean that you can have one mismatch but still it must return success.
If you check the string above, it has L, P, S, T and then E. There is no G in the pattern, but I am ok with it...
So, my question is how can you set a threshold in pattern matching above which you will return success...
ktsirig is offline   Reply With Quote
Old May 13th, 2006, 7:37 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
A match is not qualitative. It's true, or false. You may define "degree of mismatch" however you like. One way to accomplish what you ask (I don't know for sure that it's what you mean) is to conduct the tests serially and determine the ratio of successes to failures. Whether or not the resulting definition of "LIKE" is useful depends on your goals.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 13th, 2006, 7:48 AM   #3
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
You can create "optional" matches. I can't quite understand your example, so I am making my own. Plus you seem to be doing odd things with character classes.

$string="123456789"
$string2="123456"
$string3="12345"
$pattern="^[0-9]{5}[0-9]?$"
The ? mark denotes an optional numeric character class. This pattern finds "whole variables" that are either 5 or 6 numeric characters long. So, pattern will match string2 and string3.

What exactly are you trying to do? You can get "partial" matches with optional lookaround. The best approach when you have a hashed up situtation in regular expressions is to provide for yourself:

1. a clear set of examples for yourself that both match and do not match.
2. Derive these examples from from your known data universe.
3. Write a clear (English, Dutch, whatever) sentence that sets down what you're trying to do.

Go from there. Test your pattern against a bunch of trial data items.
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC