Double characters in regexp
-
I am trying to use following regular expression to parse strings:
//\s*{\s*__(?<name>[^ ]+)__\s*(?<parameters>#[^}]*)}
Strings I try to parse are for example:
// {__NAME1__}
// {__NAME2__#param1=2}
// {__NAME_3__ #param1=3, #param4=5 }My regexp should match all strings above. Name can contain one underline character (_) but not two sequential characters (__). Parameters start with first #-character and end to }-character. If there isn't any parameters my regexp goes mad and takes next next line to <name>. How I have to change my regexp so <name> doesn't allow double _-characters ?
-
I am trying to use following regular expression to parse strings:
//\s*{\s*__(?<name>[^ ]+)__\s*(?<parameters>#[^}]*)}
Strings I try to parse are for example:
// {__NAME1__}
// {__NAME2__#param1=2}
// {__NAME_3__ #param1=3, #param4=5 }My regexp should match all strings above. Name can contain one underline character (_) but not two sequential characters (__). Parameters start with first #-character and end to }-character. If there isn't any parameters my regexp goes mad and takes next next line to <name>. How I have to change my regexp so <name> doesn't allow double _-characters ?
This should do it:
//\s*{\s*__(?.*(?=__))__\s*(?\x23[^}]*)*}
-
I am trying to use following regular expression to parse strings:
//\s*{\s*__(?<name>[^ ]+)__\s*(?<parameters>#[^}]*)}
Strings I try to parse are for example:
// {__NAME1__}
// {__NAME2__#param1=2}
// {__NAME_3__ #param1=3, #param4=5 }My regexp should match all strings above. Name can contain one underline character (_) but not two sequential characters (__). Parameters start with first #-character and end to }-character. If there isn't any parameters my regexp goes mad and takes next next line to <name>. How I have to change my regexp so <name> doesn't allow double _-characters ?
Try the following. I put your two expressions into my Regex editor (http://www.radsoftware.com.au/regexdesigner/) and started playing around till I found the following to work. \s*{\s*__(?[^ ]+)__(\s*(?#[^}]*)})? It works because the second part of the expression (\s*(?#[^}]*)})? is now in an un-named optional group. Your first example, // {__NAME1__} didn't match the second part of your expression, so it failed. The other two examples matched, so they came back. This will result in an extra group being returned, but you can ignore it when you go through your matches by using the group names. Hope this helps. Hogan
-
This should do it:
//\s*{\s*__(?.*(?=__))__\s*(?\x23[^}]*)*}
Problem solved, thanks!