2012-04-19 8 views
5

मेरे पास एक खोज प्रतिलिपि है जो तारों को प्रतिस्थापित करने के लिए काम करती है। इसमें पहले से ही केस असंवेदनशील खोजों और "बच निकले" मैचों (उदाहरण के लिए खोज में 0) की खोज करने की अनुमति है।तारों को प्रतिस्थापित करने के लिए string.gsub का उपयोग करें, लेकिन केवल पूरे शब्द

अब मुझे केवल पूरे शब्दों से मेल खाने के लिए कहा गया है, मैंने% s को जोड़ने का प्रयास किया है प्रत्येक छोर, लेकिन यह एक स्ट्रिंग के अंत में शब्दों से मेल नहीं खाता है और मैं तब नहीं कर सकता कि सफेद स्पेस आइटमों को बदलने के लिए उन्हें कैसे रोकना है।

क्या मुझे फिर से करने की आवश्यकता है स्क्रिप्ट दो कार्यों मैं केस संवेदी लिए उपयोग string.find का उपयोग कर और शब्द की जाँच के लिए तर्क जोड़ सकते हैं या यह संभव पैटर्न के साथ।

और भाग निकले आइटम के रूप में दोनों के लिए खोज करने के लिए पैटर्न वापसी इस प्रकार हैं।

-- Build Pattern from String for case insensitive search 
function nocase (s) 
     s = string.gsub(s, "%a", function (c) 
      return string.format("[%s%s]", string.lower(c), 
              string.upper(c)) 
      end) 
     return s 
    end 
function strPlainText(strText) 
    -- Prefix every non-alphanumeric character (%W) with a % escape character, where %% is the % escape, and %1 is original character 
    return strText:gsub("(%W)","%%%1") 
end 

मेरे पास अब जो कुछ भी है, वह करने का एक तरीका है, लेकिन यह सुरुचिपूर्ण है। क्या कोई बेहतर तरीका है? यदि आप nocase() foo पारित

local strToString = '' 
    local strSearchFor = strSearchi 
    local strReplaceWith = strReplace 
    bSkip = false 
    if fhGetDataClass(ptr) == 'longtext' then 
     strBoxType = 'm' 
    end 
    if pWhole == 1 then 
    strSearchFor = '(%s+)('..strSearchi..')(%s+)' 
    strReplaceWith = '%1'..strReplace..'%3' 
    end 
    local strToString = string.gsub(strFromString,strSearchFor,strReplaceWith) 
    if pWhole == 1 then 
    -- Special Case search for last word and first word 
     local strSearchFor3 = '(%s+)('..strSearchi..')$' 
     local strReplaceWith3 = '%1'..strReplace 
     strToString = string.gsub(strToString,strSearchFor3,strReplaceWith3) 
     local strSearchFor3 = '^('..strSearchi..')(%s+)' 
     local strReplaceWith3 = strReplace..'%2' 
     strToString = string.gsub(strToString,strSearchFor3,strReplaceWith3) 
    end 

उत्तर

3

अब मुझे जो करना है वह करने का एक तरीका है, लेकिन यह सुरुचिपूर्ण है। क्या कोई बेहतर तरीका है?

वहाँ लुआ के पैटर्न से मेल खाते पुस्तकालय का एक गैर-दस्तावेजी सुविधा है कहा जाता Frontier Pattern, जो आप करने देगा कुछ इस तरह लिखना:

function replacetext(source, find, replace, wholeword) 
    if wholeword then 
    find = '%f[%a]'..find..'%f[%A]' 
    end 
    return (source:gsub(find,replace)) 
end 

local source = 'test testing this test of testicular footest testimation test' 
local find = 'test' 
local replace = 'XXX' 
print(replacetext(source, find, replace, false)) --> XXX XXXing this XXX of XXXicular fooXXX XXXimation XXX  
print(replacetext(source, find, replace, true)) --> XXX testing this XXX of testicular footest testimation XXX 
1

मतलब है आप, आप [fooFOO] के बजाय [सीमांत बल] [ऊ] [ऊ] चाहते हैं? यदि हां, तो आप इसे आजमा सकते हैं?

function nocase (s) 
     s = string.gsub(s, "(%a+)", function (c) 
      return string.format("[%s%s]", string.lower(c), 
              string.upper(c)) 
      end) 
     return s 
end 

और यदि आप शब्दों में एक वाक्य को विभाजित करने के लिए एक आसान तरीका चाहते हैं, तो आप इस का उपयोग कर सकते हैं: यह शब्दों पर पुनरावृति करने के लिए बहुत आसान है एक बार आप शब्द विभाजित मिल गया है

function split(strText) 
    local words = {} 
    string.gsub(strText, "(%a+)", function(w) 
            table.insert(words, w) 
            end) 
    return words 
end 

, तालिका में और प्रत्येक शब्द के खिलाफ एक पूर्ण तुलना करें।

+0

नहीं, मैं अब मैं है संसाधित खोज स्ट्रिंग का उपयोग कर रहा है क्या करने की जरूरत या एक भिन्नता और इसे केवल पूरे शब्दों से मेल खाने के लिए प्राप्त करें, इसलिए मैं "सड़क" की खोज करता हूं और यह केवल "सड़क" से मेल खाता है और "ब्रॉडस्ट्रीट" नहीं –

+0

आप nocase() और strPlainText() से वापस प्राप्त मूल्यों का उपयोग कैसे कर रहे हैं ? यह बहुत अधिक प्रासंगिक है ... –

संबंधित मुद्दे

 संबंधित मुद्दे