首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找nth字符,除非其括在方括号php中。

查找nth字符,除非其括在方括号php中。
EN

Stack Overflow用户
提问于 2014-01-11 14:27:00
回答 1查看 252关注 0票数 0

我使用以下函数在字符串中找到工作良好的第n个字符。然而,有一个例外,让我们说它是一个逗号,为此,我需要改变的是,如果昏迷是在(和),那么它不应该计算

代码语言:javascript
复制
function strposnth($haystack, $needle, $nth=1, $insenstive=0)
{
   //if its case insenstive, convert strings into lower case
   if ($insenstive) {
       $haystack=strtolower($haystack);
       $needle=strtolower($needle);
   }
   //count number of occurances
   $count=substr_count($haystack,$needle);

   //first check if the needle exists in the haystack, return false if it does not
   //also check if asked nth is within the count, return false if it doesnt
   if ($count<1 || $nth > $count) return false;


   //run a loop to nth number of occurrence
   //start $pos from -1, cause we are adding 1 into it while searching
   //so the very first iteration will be 0
   for($i=0,$pos=0,$len=0;$i<$nth;$i++)
   {   
       //get the position of needle in haystack
       //provide starting point 0 for first time ($pos=0, $len=0)
       //provide starting point as position + length of needle for next time
       $pos=strpos($haystack,$needle,$pos+$len);

       //check the length of needle to specify in strpos
       //do this only first time
       if ($i==0) $len=strlen($needle);
     }

   //return the number
   return $pos;
}

因此,我已经让regex工作起来了,它只在()外部捕获逗号,即:'/,(?=^)*(?:(x=$))/‘

这里可以看到一个活生生的例子:http://regex101.com/r/xE4jP8

但是我不知道如何使它在strpos循环中工作,我知道我需要做什么,告诉它指针有这个正则表达式异常,但我不知道如何使它工作。也许我应该放弃这个函数,用另一种方法?

仅提及我的最终结果,我希望在下一个字符串开始之前,在每6个逗号之后拆分字符串,例如:

代码语言:javascript
复制
rttr,ertrret,ertret(yes,no),eteert,ert ert,rtrter,0 rttr,ert(yes,no)rret,ert ret,eteert,ertert,rtrter,1 rttr,ertrret,ert ret,eteert,ertert,rtrter,0 rttr,ertrret,ert ret,eteert,ertert,rtrter,2 rttr,ert(white,black)rret,ert ret,eteert,ertert,rtrter,0 rttr,ertrret,ert ret,eteert,ertert,rtrter,0 rttr,ertrret,ert ret,et(blue,green)eert,ertert,rtrter,1

请注意,在字符串的下一部分开始之前,总是有一个1位数的数字(1-3),在第6个逗号之后有一个空格,但是我不能真正依赖它,因为它可能在字符串的早期出现,所以我总是可以依赖于这样一个事实,即在第一个数字之后拆分字符串,在第6个逗号之后使用空格。所以我想在这之后直接拆分字符串。

例如,上面的字符串将按如下方式拆分:

代码语言:javascript
复制
rttr,ertrret,ertret(yes,no),eteert,ert ert,rtrter,0
rttr,ert(yes,no)rret,ert ret,eteert,ertert,rtrter,1
rttr,ertrret,ert ret,eteert,ertert,rtrter,0
rttr,ertrret,ert ret,eteert,ertert,rtrter,2 
rttr,ert(white,black)rret,ert ret,eteert,ertert,rtrter,0
rttr,ertrret,ert ret,eteert,ertert,rtrter,0
rttr,ertrret,ert ret,et(blue,green)eert,ertert,rtrter,1

如果我知道如何得到角色的位置,我可以很容易地做到这一点,然后我可以使用substr来分割它,但是一个更简单的方法可能是preg_split,但我不确定它将如何工作,直到我弄清楚这个部分。

我希望我在解释时不会太困惑,我打赌我是:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-11 16:26:27

对于这类嵌套问题,正则表达式通常不是正确的工具。然而,当问题实际上并不像你的问题那么复杂时,regex就会做得很好。

试试这个:

代码语言:javascript
复制
(?:^|,)((?:[^,(]*(?:\([^)]*\))?)*)
^ start the search with a comma or the start of the string
        ^ start non capture group
           ^ search until comma or open parenthesis
                 ^ if parenthesis found then capture until 
                           ^ end of parenthesis  
                                ^ end of capture group repeat if necessary

在行动中看到它:http://regex101.com/r/eS0cX4

如您所见,这将捕获括号外逗号之间的所有内容。如果使用preg_match_all将所有这些匹配分割到数组中,则可以按任何方式将其拆分。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21063436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档