简体   繁体   English

RegEx查找PHP RegEx字符串

[英]RegEx to find a PHP RegEx string

I want to match a PHP regex string. 我想匹配一个PHP正则表达式字符串。

From what I know, they are always in the format (correct me if I am wrong): 据我所知,它们总是采用格式(如果我错了,请纠正我):

/                 One opening forward slash
the expression    Any regular expression
/                 One closing forward slash
[imsxe]           Any number of the modifiers NOT REPEATING

My expression for this was: 我对此的表达是:

^/.+/[imsxe]{0,5}$

Written as a PHP string, (with the open/close forward slash and escaped inner forward slashes) it is this: 作为PHP字符串编写的(带有打开/关闭正斜杠和转义的内部正斜杠)是这样的:

$regex = '/^\/.+\/[imsxe]{0,5}$/';

which is: 这是:

^                 From the beginning
/                 Literal forward slash
.+                Any character, one or more
/                 Literal forward slash
[imsxe]{0,5}      Any of the chars i,m,s,x,e, 0-5 times (only 5 to choose from)
$                 Until the end

This works, however it allows repeating modifiers, ie: 这有效,但是它允许重复修饰符,即:

This: ^/.+/[imsxe]{0,5}$

Allows this: '/blah/ii'
Allows this: '/blah/eee'
Allows this: '/blah/eise'
etc...

When it should not. 什么时候不应该。

I personally use RegexPal to test, because its free and simple. 我个人使用RegexPal进行测试,因为它免费且简单。

If (in order to help me) you would like to do the same, click the link above (or visit http://regexpal.com ), paste my expression in the top text box 如果(为了帮助我)您想这样做,请单击上方的链接(或访问http://regexpal.com ),然后将我的表达式粘贴到顶部文本框中

^/.+/[imsxe]{0,5}$

Then paste my tests in the bottom textbox 然后将我的测试粘贴到底部的文本框中

/^[0-9]+$/i
/^[0-9]+$/m
/^[0-9]+$/s
/^[0-9]+$/x
/^[0-9]+$/e

/^[0-9]+$/ii
/^[0-9]+$/mm
/^[0-9]+$/ss
/^[0-9]+$/xx
/^[0-9]+$/ee

/^[0-9]+$/iei
/^[0-9]+$/mim
/^[0-9]+$/sis
/^[0-9]+$/xix
/^[0-9]+$/eie

ensure you click the second checkbox at the top where it says '^$ match at line breaks (m)' to enable the multi-line testing. 确保单击顶部第二个复选框,其中显示“ ^ $匹配换行符(m)”以启用多行测试。

Thanks for the help 谢谢您的帮助

Edit 编辑

After reading comments about Regex often having different delimiters ie 阅读有关Regex的评论后,它们通常具有不同的分隔符,即

/[0-9]+/  == #[0-9]+#

This is not a problem and can be factored in to my regex solution. 这不是问题,可以考虑到我的正则表达式解决方案中。

All I really need to know is how to prevent duplicate characters! 我真正需要知道的是如何防止重复字符!

Edit 编辑

This bit isn't so important but it provides context 这一点并不重要,但它提供了上下文

The need for such a feature is simple... 需要这样的功能很简单...

I'm using jQuery UI MultiSelect Widget written by Eric Hynds. 我正在使用Eric Hynds编写的jQuery UI MultiSelect Widget

Simple demo found here 这里找到简单的演示

Now In my application, I'm extending the plugin so that certain options popup a little menu on the right when hovered. 现在,在我的应用程序中,我正在扩展插件,以便将某些选项悬停在右侧时会弹出一个小菜单。 The menu that pops up can be ANY html element. 弹出的菜单可以是任何html元素。

I wanted multiple options to be able to show the same element. 我希望多个选项能够显示相同的元素。 So my API works like this: 所以我的API是这样的:

$('#select_element_id')
// Erics MultiSelect API
.multiselect({
    // MultiSelect options
})
// My API
.multiselect_side_pane({
    menus: [
        {
            // This means, when an option with value 'MENU_1' is hovered,
            // the element '#my_menu_1' will be shown. This makes attaching
            // menus to options REALLY SIMPLE
            menu_element: $('#my_menu_1'),
            target: ['MENU_1']
        },
        // However, lets say we have option value 'USER_ID_132', I need the
        // target name to be dynamic. What better way to be dynamic than regex?
        {
            menu_element: $('#user_details_box'),
            targets: ['USER_FORM', '/^USER_ID_[0-9]+$/'],
            onOpen: function(target)
            {
                // here the TARGET can be interrogated, and the correct
                // user info can be displayed

                // Target will be 'USER_FORM' or 'USER_ID_3' or 'USER_ID_234'
                // so if it is USER_FORM I can clear the form ready for input,
                // and if the target starts with 'USER_ID_', I can parse out
                // the user id, and display the correct user info!  
            }
        }
    ]
});

So as you can see, The whole reason I need to know if a string a regex, is so in the widget code, I can decide whether to treat the TARGET as a string (ie 'USER_FORM') or to treat the TARGET as an expression (ie '/^USER_ID_[0-9]+$/' for USER_ID_234') 如您所见,在小部件代码中,我需要知道字符串是否为正则表达式的全部原因是,我可以决定将TARGET视为字符串(即“ USER_FORM”)还是将​​TARGET视为字符串。表达式(即'/ ^ USER_ID_ [0-9] + $ /'for USER_ID_234')

Unfortunately, the regexp string can be "anything". 不幸的是,正则表达式字符串可以是“任何”。 The forward slashes you talk about can be a lot of characters. 您谈论的正斜杠可能有很多字符。 ie a hash (#) will also work. 即哈希(#)也将起作用。

Secondly, to match up to 5 characters without having them double could probably be done with lookahead / lookbehind etc, but will create such complex regexp that it's faster to post-process it. 其次,最多匹配5个字符而又不让它们加倍,可以使用lookahead / lookbehind等方法来完成,但是会创建如此复杂的正则表达式,以便对其进行后处理更快。

It is possibly faster to search for the regular expression functions ( preg_match , preg_replace etc.) in code to be able to deduct where regular expressions are used. 在代码中搜索正则表达式函数( preg_matchpreg_replace等)可能更快,以便能够推断出使用正则表达式的位置。

$var = '#placeholder#';

Is a valid regular expression in PHP, but doesn't have to be one, where: 在PHP中是有效的正则表达式,但不必是其中之一:

const ESCAPECHAR = '#';
$var = 'text';
$regexp = ESCAPECHAR . $var . ESCAPECHAR;

Is also valid, but might not be seen as such. 也有效,但可能不会这样。

为了防止在修饰符部分重复,我会这样做:

^/.+/(?:(?=[^i]*i[^i]*)?(?=[^m]*m[^m]*)?(?=[^s]*s[^s]*)?(?=[^x]*x[^x]*)?(?=[^e]*e[^e]*)?)?$

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

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