Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I match namespace prefixes with regular expression

What I really want to do is to match all namespace prefixes both for tags and attributes by using a regular expression.

For example:

<foo:person bar:id="43">
  text text <nomatch:free text
  <baz:name>
    text nomatch:free> text
  </baz:name>
    text nomatch:free="44" text
    <age bae:years="44" />
    text
</foo:person>

What I want is to extract only the following prefixes: foo, baz, bad, bae, bar

I cannot find a regular expression for implementing this.

Please take account that the regular expression should not have hardcoded the names foo, baz, bad, bae, bar...

like image 601
user3688492 Avatar asked Feb 03 '26 05:02

user3688492


1 Answers

Something like this?

[<]([^:]+)

will put the prefix (after the angle bracket and before the colon) into group 1.

I see that I misinterpreted the question, after looking more closely.

[<](((\w+):\w+)(\s+(\w+):\w+="\d+"\s*[\/])?)>

which will have a result in groups $3 and possibly $5. I think this will do it.

Whoops, missed a ?

 [<](((\w+):\w+)(\s+(\w+):\w+="\d+"\s*[\/]?)?)>
like image 72
Steeve McCauley Avatar answered Feb 04 '26 21:02

Steeve McCauley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!