donglv7097 2014-01-24 00:15
浏览 81
已采纳

PHP正则表达式(包括特定于括号和任何字母/数字/符号,取决于字符串中的位置)

After a few hours of experimenting with http://www.phpliveregex.com/ - I have become rather stuck. I am looking for a regular expression that would satisfy the following example criteria:

arrname = array('blackberry', 'apple', 'orange', 'mandarin');
arrname = array('****11111', '2%%%2', '3$$$$33', '444£££44');

So essentially it boils down to the following pattern:

[arrname = array('] [any characters] [', '] [any characters] [', '] [any characters] [', '] [any characters] [');]

Where any character is literally any (letter / number / symbol) - in any order (but requires a length of at least 1 character) - hence the need for a regular expression using the preg_match() function.

My trouble is making the regular expression match the pattern stated above (repeated below).

[arrname = array('] [any characters] [', '] [any characters] [', '] [any characters] [', '] [any characters] [');]

UPDATE:

Having tried to implement preg_match() I've failed and am obviously missing something really stupid (errors listed below). Any idea's?

First (using double quotes on the expression)

$pattern = "arrname = array\('([^']+)', '([^']+)', '([^']+)', '([^']+)'\);";
preg_match($pattern, $data, $matches);

Gives me the error Warning: preg_match(): Delimiter must not be alphanumeric or backslash

Second (using single quote on the expression)

$pattern = 'arrname = array\('([^']+)', '([^']+)', '([^']+)', '([^']+)'\);';
preg_match($pattern, $data, $matches);

Gives me the error Parse error: syntax error, unexpected '('

  • 写回答

2条回答 默认 最新

  • doujiling4377 2014-01-24 02:38
    关注

    As an aside comment, I will describe regex engine behaviour with two different patterns step by step. Keep in mind that this is only a short representation, in real life strings are processed character by character. The goal of this is to see the regex engine road.

    string: arr = array('cherry', 'apple');

    pattern 1: arr = array\('(.+)', '(.+)'\);

    1 | arr = array('cherry', 'apple');  | arr = array\('
    2 | arr = array('cherry', 'apple');| arr = array\('(.+)

    Since there is no ' after the ;, the regex engine must backtrack character by character to find a match. For each backtrack position the end of the pattern is tested. This is reason why I count each backtrack position as a step.

    3 | arr = array('cherry', 'apple');| arr = array\('(.+)
    4 | arr = array('cherry', 'apple');| arr = array\('(.+)
    5 | arr = array('cherry', 'apple');| arr = array\('(.+)

    The ' is found, the regex engine stop to backtrack and continue

    6 | arr = array('cherry', 'apple');| arr = array\('(.+)',

    There's no , after the ' the RE restart backtracking to find another '

    7 | arr = array('cherry', 'apple');| arr = array\('(.+)
    ...
    13 | arr = array('cherry', 'apple');| arr = array\('(.+)

    another ' is found

    14 | arr = array('cherry', 'apple');| arr = array\('(.+)'

    but not followed by a , too

    15 | arr = array('cherry', 'apple');| arr = array\('(.+)
    16 | arr = array('cherry', 'apple');| arr = array\('(.+)
    17 | arr = array('cherry', 'apple');| arr = array\('(.+)

    another ' is found followed by a , '

    18 | arr = array('cherry', 'apple');| arr = array\('(.+)', '
    19 | arr = array('cherry', 'apple');| arr = array\('(.+)', '(.+)

    There is no ' after the ;, ...

    20 | arr = array('cherry', 'apple');| arr = array\('(.+)', '(.+)
    21 | arr = array('cherry', 'apple');| arr = array\('(.+)', '(.+)
    22 | arr = array('cherry', 'apple');| arr = array\('(.+)', '(.+)

    The ' is found, followed by all the literals at the end of the pattern

    23 | arr = array('cherry', 'apple');| arr = array\('(.+)', '(.+)'\);

    pattern 2: arr = array\('([^']+)', '([^']+)'\);

    1 | arr = array('cherry', 'apple');  | arr = array\('
    2 | arr = array('cherry', 'apple'); | arr = array\('([^']+)

    Now the regex engine is forced to stop before the ' since the character class contains all characters except the '

    3 | arr = array('cherry', 'apple'); | arr = array\('([^']+)', '
    4 | arr = array('cherry', 'apple'); | arr = array\('([^']+)', '([^']+)

    The RE stop before the ' for the same reason

    5 | arr = array('cherry', 'apple'); | arr = array\('([^']+)', '([^']+)'\);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?