I'm trying to extract the following pattern {#56DS1e5R9w7v} which is :

  • {
  • Hash
  • a-z, A-Z, 0-9 ( not necessarily an alphanumeric string )
  • }

Any ideas please?

Thank you


In what way is a-z A-Z 0-9 "not necessarily an alphanumeric string"?

Written by Jon Skeet

Means it may be only numbers or characters or both.

Written by David

Accepted Answer

Try this pattern:


It should match all characters that are not }, and place the result in a captured group. You may want to change [^}]* to \w* or [A-Za-z0-9]* if that's problematic.

Example (also on ideone.com):

$str = "hello {#56DS1e5R9w7v} good people";
preg_match_all("/\{#([^}]*)\}/", $str, $matches);
Written by Kobi
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki