arrays - Is there a Perl shortcut to count the number of matches in a string? -


मान लीजिए मेरे पास है:

  मेरी $ string = "one.two.three.four ";  

पैटर्न को मैच (3) मिलते समय की संख्या प्राप्त करने के लिए मुझे संदर्भ के साथ कैसे खेलना चाहिए? क्या यह एक-लाइनर का उपयोग कर किया जा सकता है?

मैंने इसे करने की कोशिश की:

  मेरी ($ संख्या) = स्केलर ($ string = ~ /। / Gi);  

मैंने सोचा था कि कोष्ठक को $ number के आस-पास रखकर, सरणी के संदर्भ को बल देता, और scalar के उपयोग से, I गिनती करना हालांकि, मुझे जो भी मिलता है वह 1 है।

यह regex को itself में डालता है स्केलर संदर्भ, जो कि आप क्या चाहते हैं नहीं है इसके बजाय, regex को सूची के संदर्भ में डालकर (मैचों की संख्या प्राप्त करने के लिए) और उस को स्केलर संदर्भ में डाल दिया।

  मेरी $ number = () = $ string = ~ /। / / G;  

Comments

Popular posts from this blog

iphone - How do I make a UIPickerView in a UIActionSheet -

java - Is there an object like a "Set" that can contain only unique string values, but also contain a count on the number of occurrences of the string value? -

MVP, design question -