0

I'm facing a problem when I'm trying to parse string containing FSM defined as:

{{states},{symbols}, {transitions}, starting_symbol, {set of finishing states}}

example:

{{start, mid, end}, {'a', 'b'}, { start 'a' -> end}, start, {end}}

As opposed to writing a parser here, I would like to parse this via regular expressions.

My problem lies in understanding regex matching groups and how to break string such as "start, mid, end" into array [ "start", "mid", "end" ] (assuming delimiter ,).

I understand that using builtin csv functions might be better idea, but example is bit more complex (albeit for simplicity sake lets assume that each member of set can be matched by \w.

Any help would be appreciated.

2
  • 3
    That doesn't look like a csv at all Commented Mar 18, 2015 at 15:18
  • to clarify, I only want to parse "start, mid, end" part. Commented Mar 18, 2015 at 15:29

1 Answer 1

0

Assuming I understood correctly what you need, this should do the trick:

<?php

$string = "{{start, mid, end}, {'a', 'b'}, { start 'a' -> end}, start, {end}}";

preg_match( "#\{\{(.*?)\}#", $string, $matches );

$parts = split(',', $matches[1] );

echo '<pre>';
print_r( $parts );
echo '</pre>';
Sign up to request clarification or add additional context in comments.

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.