Skip to main content
Post Reopened by SuperBiasedMan, rolfl, Malachi, Mast, janos
Make sample code quoted.
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418
/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *  ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */
/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *    ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */
<?php

array(
    [
        'name' => 'Tag',
        'value' => 'name name'
    ],
    [
        'name' => 'annotation',
        'value' => 'bee'
    ],
    [
        'name' => 'aaf',
        'value' => 'dsfsd fgdg'
    ]
    // ... and so on... (see live example)
);
<?php

array(
  [
      'name' => 'Tag',
      'value' => 'name name'
  ],
  [
      'name' => 'annotation',
      'value' => 'bee'
  ],
  [
      'name' => 'aaf',
      'value' => 'dsfsd fgdg'
  ]
  // ... and so on... (see live example)
);
/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *  ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */
<?php

array(
    [
        'name' => 'Tag',
        'value' => 'name name'
    ],
    [
        'name' => 'annotation',
        'value' => 'bee'
    ],
    [
        'name' => 'aaf',
        'value' => 'dsfsd fgdg'
    ]
    // ... and so on... (see live example)
);
/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *    ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */
<?php

array(
  [
      'name' => 'Tag',
      'value' => 'name name'
  ],
  [
      'name' => 'annotation',
      'value' => 'bee'
  ],
  [
      'name' => 'aaf',
      'value' => 'dsfsd fgdg'
  ]
  // ... and so on... (see live example)
);
updated reg expr
Source Link
itsjavi
  • 137
  • 4

I am writing a regular expression for parsing PHP annotations in a flexible way. I'd like to accomplish all the goals listed above with one single expression. So I would appreciate any suggestions about the quality of it (in terms of corner cases, performance, best practices and correctness).

PCRE expression:

/[\*\s]*@(?P<name>\w+[\\\w]*?)\s(?:\s|\()
(?P<value>
  (?:
    [\w\s\"\<\>\_\#\=\[\/\w\s\"\<\>\_\#\=\-\.\'\{\}:;,\*\(\)\[\]]*[^\R\*\s\/\)])
  )?
  (?:\s  |  $$|\)
 )/gsxmu

Goals of the regular expression:

  • List all annotations with their values
  • Values can be multi-lined and have markup (html, json or markdown)
  • The initial space + * of each line should be removed from the value
  • Many annotations can be in the same line
  • Annotation names can be namespaced

Sample PHPDoc string:

/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *  ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */

Expected result:

<?php

array(
    [
        'name' => 'Tag',
        'value' => 'name name'
    ],
    [
        'name' => 'annotation',
        'value' => 'bee'
    ],
    [
        'name' => 'aaf',
        'value' => 'dsfsd fgdg'
    ]
    // ... and so on... (see live example)
);

A live demo can be found herehere.

I am writing a regular expression for parsing PHP annotations in a flexible way. I'd like to accomplish all the goals listed above with one single expression. So I would appreciate any suggestions about the quality of it (in terms of corner cases, performance, best practices and correctness).

PCRE expression:

/[\*\s]*@(?P<name>\w+[\\\w]*?)\s?
(?P<value>
  (?:
    [\w\s\"\<\>\_\#\=\-\.\'\{\}:;,\*\(\)\[\]]*[^\R\*\s\/]
  )
  (?:\s  |  $)
 )/gsxmu

Goals of the regular expression:

  • List all annotations with their values
  • Values can be multi-lined and have markup (html, json or markdown)
  • The initial space + * of each line should be removed from the value
  • Many annotations can be in the same line
  • Annotation names can be namespaced

Sample PHPDoc string:

/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *  ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */

Expected result:

<?php

array(
    [
        'name' => 'Tag',
        'value' => 'name name'
    ],
    [
        'name' => 'annotation',
        'value' => 'bee'
    ],
    [
        'name' => 'aaf',
        'value' => 'dsfsd fgdg'
    ]
    // ... and so on... (see live example)
);

A live demo can be found here.

I am writing a regular expression for parsing PHP annotations in a flexible way. I'd like to accomplish all the goals listed above with one single expression. So I would appreciate any suggestions about the quality of it (in terms of corner cases, performance, best practices and correctness).

PCRE expression:

/[\*\s]*@(?P<name>\w+[\\\w]*?)(?:\s|\()
(?P<value>
  (?:[\/\w\s\"\<\>\_\#\=\-\.\'\{\}:;,\*\(\)\[\]]*[^\R\*\s\/\)])
)?
(?:\s | $|\))/gsxmu

Goals of the regular expression:

  • List all annotations with their values
  • Values can be multi-lined and have markup (html, json or markdown)
  • The initial space + * of each line should be removed from the value
  • Many annotations can be in the same line
  • Annotation names can be namespaced

Sample PHPDoc string:

/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *  ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */

Expected result:

<?php

array(
    [
        'name' => 'Tag',
        'value' => 'name name'
    ],
    [
        'name' => 'annotation',
        'value' => 'bee'
    ],
    [
        'name' => 'aaf',
        'value' => 'dsfsd fgdg'
    ]
    // ... and so on... (see live example)
);

A live demo can be found here.

more specific description and title
Source Link
itsjavi
  • 137
  • 4

Regular Expression for parsing PHP annotations containing multiple lines and markup

I am writing a regular expression for parsing PHP annotations in a flexible way. Currently I'd like to accomplish all the goals listed above with one single expression does not trim whitespace or newlines at the end of the value. Any improvements toSo I would appreciate any suggestions about the expression are welcomequality of it (in terms of corner cases, performance, best practices and correctness).

PCRE expression:PCRE expression:

/[\*\s]*@(?P<name>\w+[\\\w]*?)\s?
(?P<value>
  (?:
    [\w\s\"\<\>\_\#\=\-\.\'\{\}:;,\*\(\)\[\]]*[^\R\*\s\/]
  )
  (?:\s  |  $)
)/gsxmu

Sample string:Goals of the regular expression:

  • List all annotations with their values
  • Values can be multi-lined and have markup (html, json or markdown)
  • The initial space + * of each line should be removed from the value
  • Many annotations can be in the same line
  • Annotation names can be namespaced

Sample PHPDoc string:

/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *  ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */

Expected result:

<?php

array(
    [
        'name' => 'Tag',
        'value' => 'name name'
    ],
    [
        'name' => 'annotation',
        'value' => 'bee'
    ],
    [
        'name' => 'aaf',
        'value' => 'dsfsd fgdg'
    ]
    // ... and so on... (see live example)
);

A live demo can be found here.

Regular Expression for parsing PHP annotations

I am writing a regular expression for parsing PHP annotations in a flexible way. Currently the expression does not trim whitespace or newlines at the end of the value. Any improvements to the expression are welcome.

PCRE expression:

/[\*\s]*@(?P<name>\w+[\\\w]*?)\s?
(?P<value>
  (?:
    [\w\s\"\<\>\_\#\=\-\.\'\{\}:;,\*\(\)\[\]]*[^\R\*\s\/]
  )
  (?:\s  |  $)
)/gsxmu

Sample string:

/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *  ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */

Expected result:

<?php

array(
    [
        'name' => 'Tag',
        'value' => 'name name'
    ],
    [
        'name' => 'annotation',
        'value' => 'bee'
    ],
    [
        'name' => 'aaf',
        'value' => 'dsfsd fgdg'
    ]
    // ... and so on... (see live example)
);

A live demo can be found here.

Regular Expression for parsing PHP annotations containing multiple lines and markup

I am writing a regular expression for parsing PHP annotations in a flexible way. I'd like to accomplish all the goals listed above with one single expression. So I would appreciate any suggestions about the quality of it (in terms of corner cases, performance, best practices and correctness).

PCRE expression:

/[\*\s]*@(?P<name>\w+[\\\w]*?)\s?
(?P<value>
  (?:
    [\w\s\"\<\>\_\#\=\-\.\'\{\}:;,\*\(\)\[\]]*[^\R\*\s\/]
  )
  (?:\s  |  $)
)/gsxmu

Goals of the regular expression:

  • List all annotations with their values
  • Values can be multi-lined and have markup (html, json or markdown)
  • The initial space + * of each line should be removed from the value
  • Many annotations can be in the same line
  • Annotation names can be namespaced

Sample PHPDoc string:

/**
 * Description
 * 
 * @Tag name name @annotation beee @aaf dsfsd fgdg
 * @Tag name name {"json":"dfsf"}
 * @Tag asdasd <html> #markdown ==markdown== __markdown__
 * - markdown
 * > mark 1
.mark
"string"
'string'
 * @Annotation()
 * @Tag name name @annotation beee @aaf dsfsd fgdg <markdown> #markdown ==markdown==
 * @a() @b("name") @c()
 * @Annotation\Name("var1()", "var2") @n("name()_name")
 * @Annotation(["var1", "var2"], "var3")
 * @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
 * @Annotation(
 *  ["GET", "POST"]
   ) @Name({"name": "Tomas"})
 * @Tag name name
 */

Expected result:

<?php

array(
    [
        'name' => 'Tag',
        'value' => 'name name'
    ],
    [
        'name' => 'annotation',
        'value' => 'bee'
    ],
    [
        'name' => 'aaf',
        'value' => 'dsfsd fgdg'
    ]
    // ... and so on... (see live example)
);

A live demo can be found here.

Post Closed as "Not suitable for this site" by Ethan Bierlein, Kaz, Malachi, Mast, Bjørn-Roger Kringsjå
added sample string and partial expected result sample
Source Link
itsjavi
  • 137
  • 4
Loading
added 87 characters in body
Source Link
itsjavi
  • 137
  • 4
Loading
deleted 105 characters in body
Source Link
Ethan Bierlein
  • 15.8k
  • 4
  • 59
  • 146
Loading
Source Link
itsjavi
  • 137
  • 4
Loading