![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 54
Rep Power: 4
![]() |
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... |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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]?$"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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|