开发者

Extract a special hash from a string

开发者 https://www.devze.com 2023-02-09 01:07 出处:网络
I\'m trying to extract the following pattern {#56DS1e5R9w7v} which is : { Hash a-z, A-Z, 0-9 ( not necessarily an alphanumeric string )

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

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

Any i开发者_C百科deas please?

Thank you


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);


Something like ({#[a-z0-9]+?}) ?

preg_match('/(\{#[a-z0-9]+?\})/i', $sString)


What about:

 <?php

     $code = '{#56DS1e5R9w7v}';
     $matches = preg_match('/^\{#[a-zA-Z0-9]+\}$/', $code); 

 ?>

This makes sure the string begins with a { the second char must be a #, the 3th char until } must be alpha numeric and it must end with a }.

Hope this helped!

0

精彩评论

暂无评论...
验证码 换一张
取 消