Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to use double curly Brackets in my RegEx?

Tags:

regex

xslt

saxon

I'm running a little regular expression in one of my xsl-transformations (xsl:analyze-string) and came across this effect that made me rather uncomfortable because I didn't really find any explanation...

I was searching for Non-Breaking-Spaces and En-Spaces, so I used the \p{Z} construct. According to many examples in the XSLT 2.0 Programmers Reference by Michael Kay, this should work. RegExBuddy also approves :)

Now my SaxonHE9.4N tells me

Error in regular expression: net.sf.saxon.trans.XPathException: expected ({)

After several trials and errors I simply doubled the Brackets \p{{Z}} ... and it worked!? But this time RegExBuddy disapproves!

Can someone give me an explanation of this effect? I couldn't find anything satisfying on the internet...

Thanks in advance!

Edit: I tried the same thing inside of a replace() function and the double bracket version didn't work. I had to do it with single brackets!

like image 813
Der Schmu Avatar asked Jan 30 '14 12:01

Der Schmu


People also ask

What do curly brackets do in regex?

The curly brackets are used to match exactly n instances of the proceeding character or pattern. For example, "/x{2}/" matches "xx".

Do I need to escape curly braces in regex?

To match literal curly braces, you have to escape them with \ . However, Apex Code uses \ as an escape, too, so you have to "escape the escape". You'll need to do this almost every time you want to use any sort of special characters in your regexp literally, which will happen more frequently than not.

What does double curly brackets do in HTML?

The double curly brackets are not HTML but scripting code. The term inside, interest, is a placeholder, sort of like the name and address in a form letter. The string {{interest}} will be replaced when the HTML template is converted into straight HTML that is sent over the network to the user.

What does double curly braces mean in Python?

The curly braces are part of Django Template Language. The part encapsulated between double curly braces {{ }} is nothing but a variable. That's how DTL, Jinja2 and other template languages work. They have their own set of rules which translates the template in to python and later to HTML code.


1 Answers

In an attribute value template, curly braces are special syntax indicating an XPath expression to be evaluated. If you want literal curly braces, you have to escape them by doubling:

An attribute value template consists of an alternating sequence of fixed parts and variable parts. A variable part consists of an XPath expression enclosed in curly brackets ({}). A fixed part may contain any characters, except that a left curly bracket must be written as {{ and a right curly bracket must be written as }}.

Note:

An expression within a variable part may contain an unescaped curly bracket within a StringLiteral XP or within a comment.

Not all attributes are AVTs, but the regex attribute of analyze-string is:

Note:

Because the regex attribute is an attribute value template, curly brackets within the regular expression must be doubled. For example, to match a sequence of one to five characters, write regex=".{{1,5}}". For regular expressions containing many curly brackets it may be more convenient to use a notation such as regex="{'[0-9]{1,5}[a-z]{3}[0-9]{1,2}'}", or to use a variable.

(Emphasis added, in both quotes.)

like image 167
LarsH Avatar answered Nov 15 '22 11:11

LarsH