1

I have the below regex expression and I'm trying to break it into two parts, there might not always be the the two parts though.

Section Level: Very Low. Type: Animal.

I'm using the below to break it by characters but I need to be able to break by the string, identify the level and type so they can be returned into two separate database columns. For example I need to check the string and see if there is anything matching 'Section Level: {text}.' and insert it into the level column in the database and the same for type, I need to check the string for 'Type: {text}.' and insert it into a type column.

(?<=\:)(.*?)(?=\.)

I've been able to get the first piece out using (\w*)(.*?)(?=\.) but still can't for the life of me get the second part.

5
  • and what does not work? Commented Mar 29, 2013 at 9:11
  • It looks like you want us to write the code for you. Commented Mar 29, 2013 at 9:13
  • Like I said in the post, I need to break it up based on the text between 'Level:' and the next full stop and then also break it up on 'Type:' and the next full stop after type but there wont always be a type or a level. Commented Mar 29, 2013 at 9:13
  • @DmitryDovgopoly, how did you even get that from my post, I clearly explained what I'm trying to do and what I've already tried to solve my issue. I'm just asking to be pointed in the right direction, I never said I want you to write the code! Commented Mar 29, 2013 at 9:16
  • Sorry, i misinterpreted your perpose because your regular expression is perfectly fits for your string example, so i actually don't see how we could help you, exept writing the code in php. Cold you please provide test cases, where you regex is not working Commented Mar 29, 2013 at 9:19

2 Answers 2

2

Following regex should work for you:

(?=\w)(?:Section\s+Level:\s*([^.]+)\.)?\s*(?:Type:\s*([^.]+)\.)?

With both Level and Type fields as optional fields.

Live Demo: http://www.rubular.com/r/GpSHs69YHy

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for that, Slight change to it, I forgot to add in the other piece of text, once I do this it seems to return 3 array elements, I've changed the regex at this link, rubular.com/r/9oODd88KIg
You should really consider not using a regex for this as you will always have expand it for every new word. Also this regex, due to making nearly everything optional, will match anything containing at least a dot, like "......", ".aaaaaaaa", etc. The strtok alternative seems more appropriate for your needs.
@migg Pls pay close attention to the regex before claiming will match anything containing at least a dot
@atallon: Your suggested regex at rubular.com/r/9oODd88KIg looks good to me.
Ok... maybe I should clarify that it generates "a lot of empty matching groups" for anything containing at least a dot ;)
|
1

I'd probably use strtok rather than regular expressions:

$string = 'Level: Very Low. Type: Animal.';

$parts = array();

for ($s = strtok($string, '.'); $s; $s = strtok('.'))
{

  $array = explode(':', $s);

  $parts[trim($array[0])] = trim($array[1]);

}

print_r($parts);

That would give you:

Array
(
    [Level] => Very Low
    [Type] => Animal
)

The benefit is that if you ever added something else, say Gender: Male then you wouldn't have to modify the code (much).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.