1

Can I extract the string aaa without using subpatterns (but still using regexp)?

$str = 'abc#aaa#abc';
preg_match('/#(.*)#/', $str, $matches);
print_r($matches);

Update

  • lookahead and lookbehind is a second solution, any other ?

I'm just trying to figure out some regexp alternatives !

2
  • Just to be clear: you want to capt^H^H^H^H get the text between pound sign using a regex rather than substring and indexOf calls, but not use a subpattern? Commented Aug 3, 2011 at 19:08
  • Qtax has a good answer; I will try to think of others. :) Commented Aug 3, 2011 at 19:23

2 Answers 2

4

/(?<=#).*?(?=#)/

But why wouldn't you want to use a capturing group?

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

1 Comment

usually you just need to find a substring and then remove the boundaries
1

Alternative to look-ahead / look-behind assertions:

echo preg_replace('/^.*?#|#.*?$/s', '', 'abc#aaa#abc');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.