简体   繁体   English

匹配正则表达式

[英]Matching a regex

My regex skills are poor. 我的正则表达式技能很差。 For my js/css to show the correct menu I am adding in a "selected" class to my li so that the js knows which is the current item (so it can display the rest of the drop down). 为了让我的js / css显示正确的菜单,我在“ li”中添加了一个“ selected”类,以便js知道哪个是当前项目(以便它可以显示其余的下拉菜单)。

My problem is matching the correct uri string in codeigniter: 我的问题是在codeigniter中匹配正确的uri字符串:

<li <? if((strstr($this->uri->uri_string(),"rfid_finder/finder")) || (strstr($this->uri->uri_string(),"finder"))) {?>class="selected"<?}?>  rel="home"> <?= anchor('finder','HOME')?></li>

I was using this method initially but my routes are now a bit more complex to allow for searching and pagination. 我最初使用的是这种方法,但是现在我的路线更加复杂,无法进行搜索和分页。

I need a regex that would match all of the following routes: 我需要一个匹配以下所有路由的正则表达式:

$route['finder/(:any)/(:any)/(:num)'] = "rfid_finder/finder/$1/$2/$3";
$route['finder/(:any)/(:any)'] = "rfid_finder/finder/$1/$2";
$route['finder/(:any)'] = "rfid_finder/finder/$1";
$route['finder'] = 'rfid_finder/finder';

but when a user visits: 但是当用户访问时:

rfid_finder/search_form

the first menu is not given the selected class. 没有为第一个菜单提供所选的类别。

Update 更新资料

I want first code snippet to match the routes and not the rfid_finder/search route- I have a second line of code which matches the rfid_finder/search_form route. 我希望第一个代码段匹配路由而不是rfid_finder/search路由-我有第二行代码匹配rfid_finder / search_form路由。 my problem lies in trying to capture the route using (strstr($this->uri->uri_string(),"finder") it matches all my routes even the rfid_finder/search_form 我的问题在于尝试使用(strstr($this->uri->uri_string(),"finder")捕获路线(strstr($this->uri->uri_string(),"finder")它甚至与rfid_finder/search_form都匹配我的所有路线

Hmm, let me know if this works for you. 嗯,让我知道这是否适合您。 It seems to work in my tests. 它似乎在我的测试中有效。

preg_match('|^(rfid_finder/)?finder/?([a-z0-9]+?)?(/[a-z0-9]+?)?(/\d+?)?$|i', $string)

Here's my testing example: 这是我的测试示例:

$route = array();
$route['finder/(:any)/(:any)/(:num)'] = "rfid_finder/finder/$1/$2/$3";
$route['finder/(:any)/(:any)'] = "rfid_finder/finder/$1/$2";
$route['finder/(:any)'] = "rfid_finder/finder/$1";
$route['finder'] = 'rfid_finder/finder';


$route['finder/2313'] = 'rfid_finder/finder';
$route['finder/asd/dsda'] = 'rfid_finder/finder';
$route['rfid_finder/finder/dd/122'] = 'rfid_finder/finder';
$route['rfid_finder/finder/dd/122/qwewe'] = 'rfid_finder/finder';
$route['rfid_finder/finder/dd/asdsad/333'] = 'rfid_finder/finder';

foreach ($route as $string=>$match) {
    if ( preg_match('|^(rfid_finder/)?finder/?([a-z0-9]+?)?(/[a-z0-9]+?)?(/\d+?)?$|i', $string) ) {
        echo $string.' - yes';
    } else {
        echo $string.' - no';
    }
    echo '<br />';
}

exit();

It searches the string to make sure it starts with either finder or rfid_finder (i wasn't really sure which you prefered. if you want it to only start with rfid_finder just remove the parentheses around rfid_finder and the trailing ? before finder) 它搜索字符串以确保它以finder或rfid_finder开头(我不确定您是哪一个。如果只希望以rfid_finder开头,只需在finder之前删除rfid_finder周围的括号和尾部的?)即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM