Domanda

I wrote a regex to parse out a string in the form of:

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

with this:

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

But I ran into a problem when a job came in this form instead:

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

So my question is, how can I take in everything before the numeric job number as the 'jobTitle', the numeric portion as the 'jobCode', and everything after that as the 'location'?

For example

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
È stato utile?

Soluzione

You could be looking for something like:

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

Altri suggerimenti

With this simple regex, your strings will be in Groups 1, 2 and 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>";
    }

OUTPUT:

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
        )

)

If you want to extract all the fields, you can use this:

^(?<title>\D+) \((?<id>\d+)\)(?: (?<desc>[^,]+),)? (?<city>[^,]+), (?<country>[^,]+)$
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top