1

I am tryin to figure out how to find first occurence of specified character (lets say =) in string. This is easy, but i want position of that = only if its NOT in quotes.

For example, in this case:

  foo = bar 

I want position of first =, but in this case:

  "foo = bar" = baz

I want position of the second =.

I found similar question here, but I need position, not splitting. And I must be able to deal with escaped quotes \" or \' so I think I wont be able to use string based approach to this problem.

One of my ideas was using lex. analysis with regexp based syntax analysis, which can find first occurence of = for me, but it seems rather heavy :)

Community
  • 1
  • 1
Pirozek
  • 1,250
  • 4
  • 16
  • 25

1 Answers1

4

This should be possible with a regular expression because quotes cannot be nested, something like the following:

^([^="]|"(\\"|[^"])*")*(=)

You'll need to find the index position of the final matched group (=).

Demo: http://www.rubular.com/r/jt5CSIaOjo

mellamokb
  • 56,094
  • 12
  • 110
  • 136
  • 1
    the position can be found in php like this: `preg_match('/^([^="]|"(\\"|[^"])*")*(=)/',$searchstring,$matches,PREG_OFFSET_CAPTURE);` – Jan Prieser Jul 13 '12 at 14:13