Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,
Can you please tell what is the meaning of this particular pattern.
I am just being a beginner and found this as an example in one of the site
providing example for reg expression through regex.h

char *PatternSearch = "[?&]XYZ=\\([[:alnum:]]*\\)";


Appreciate your response if you could explain what does this pattern look for with an example


Regards,
Posted

1 solution

[?&] is one of ? or &
XYZ= is just that exact text (case sensitive)
\\( is the opening bracket character (. The \\ is escaped by the compiler, so it is passing \( into the regex
[:alnum:] is shorthand for A-Za-z0-9 (no square brackets) which expands to ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789. Note that the [] is part of this expansion.
[[:alnum:]] searches for [A-Za-z0-9]
[[:alnum:]]* searches for any number of [[:alnum:]] (see previous)
\\) is the closing bracket character ). Again \\ is escaped by the compiler, passing \( in as the regex

So, it is searching for
C++
?XYZ=(/*any characters in here*/)

or
C++
&XYZ=(/*any characters in here*/)
 
Share this answer
 
Comments
[no name] 30-Oct-11 11:41am    
thank you very much
[no name] 30-Oct-11 11:42am    
but what is the difference between [:alnum:] and [[:alnum:]]
Andrew Brock 30-Oct-11 11:52am    
[:alnum:] expands to JUST "A-Za-z0-9", not "[A-Za-z0-9]". Since you want it as any character in the set, you need to add the extra [] around it to get it to "[A-Za-z0-9]".
This allows you to do something like "[[:alnum:]_]" which will also include an underscore character in the search.
[no name] 30-Oct-11 12:00pm    
please correct me if I am wrong.
So [[:alnum:]] means only [a] or [abcd] aswell. Want to know how many characters i can repeat through this pattern.
Andrew Brock 30-Oct-11 12:05pm    
The full expansion of "[[:alnum:]]" is "[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789]".
This will match any single upper or lower case character or any single digit.
It is then followed by a * in your regex, which means that there can be 0 or more of these. So "[[:alnum:]]*" will match any combination of numbers and letters

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900