tags:

views:

38

answers:

1

This is a follow up from a previous question.

The regular expression is working fantastic however I need to be able to find out the exact location of the pattern occurrence. Like for example if my regular expression were to extract instances of the substring 'bob' from the following string:

This is abobsled

I want to also know the exact character position of the first occurrence of the pattern in the string i.e in this case its at the 10th character position - is this possible? I'm using php here.

==========================

Thanks I think I'm stuck again though - currently however this is what I want to do. Lets say I have a template of this nature:

Hello <=name=>

Take Care

From <=sender=>

Now thats easy as all the details are one words however in case the data to replace a tag extends over multiple lines I want to set it sup so it maintains a bit of formatting like currently the regular expression I use if I use it to replace a tag with something thats multiline I get the following:

Numbers: <=numbers=>

becomes

Numbers: number1
number2

When I want something like:

Numbers: Number1
         Number2

How can I do this :(

+5  A: 

see: flag PREG_OFFSET_CAPTURE in http://docs.php.net/preg_match_all and/or http://docs.php.net/preg_match

E.g.

<?php
$subject = 'This is abobsled';
preg_match('/b.b/', $subject, $m, PREG_OFFSET_CAPTURE);
var_dump($m)

prints

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(3) "bob"
    [1]=>
    int(9)
  }
}
It starts counting with 0, so the 10th character is at position 9.

VolkerK
Great - but please check my updated question...
Ali
@Ali If that's a different question you should ask a new one. It may be easier to use YAML or similar: http://stackoverflow.com/search?q=yaml+ini
banzaimonkey