如果内容不是数字,正则表达式忽略匹配?

时间:2014-04-17 22:59:17

标签: php regex preg-match

我写了一个正则表达式来解析一个字符串:

Job Title (<numeric job number>) Location, State, Country

用这个:

(?P<jobTitle>[a-zA-Z0-9,\:\/\s]+)[\s]+\((?P<jobCode>[0-9]+)\)[\s]+(?P<location>[a-zA-Z0-9,\s]+)

但是当我的工作以这种形式出现时,我遇到了一个问题:

Job Title (extra information) (<numeric job number>) Location, State, Country

所以我的问题是,如何将数字作业编号之前的所有内容作为“jobTitle”,将数字部分作为“jobCode”,以及之后的所有内容作为“位置”?

例如

Super Cool Job (12345) Cool Place, California, United States
jobTitle => Super Cool Job
jobCode => 12345
location => Cool Place, California, United States

Another Cool Job (Not in california) (54321) Paris, France
jobTitle => Another Cool Job (Not in california)
jobCode => 54321
location => Paris, France

3 个答案:

答案 0 :(得分:1)

你可能正在寻找类似的东西:

(.*\S)\s+\((\d+)\)\s+(\S.*)

答案 1 :(得分:1)

使用这个简单的正则表达式,您的字符串将位于第1组,第2组和第3组

    $jobs='Super Cool Job (12345) Cool Place, California, United States
Another Cool Job (Not in california) (54321) Paris, France';

$regex = '/^(?m)(.*?)\s+\((\d+)\)\s+(.*)$/';

if(preg_match_all($regex,$jobs,$matches, PREG_SET_ORDER)) {
    echo "<pre>";
    print_r($matches);
    echo "</pre>";
    }

<强>输出:

Array
(
    [0] => Array
        (
            [0] => Super Cool Job (12345) Cool Place, California, United States
            [1] => Super Cool Job
            [2] => 12345
            [3] => Cool Place, California, United States
        )

    [1] => Array
        (
            [0] => Another Cool Job (Not in california) (54321) Paris, France
            [1] => Another Cool Job (Not in california)
            [2] => 54321
            [3] => Paris, France
        )

)

答案 2 :(得分:0)

如果要提取所有字段,可以使用:

^(?<title>\D+) \((?<id>\d+)\)(?: (?<desc>[^,]+),)? (?<city>[^,]+), (?<country>[^,]+)$