开发者

"Grouped expression is missing ')'", except it isn't

Using VS2010's find dialog, I'm using the following regex: (?'outer'^div\#.+) input$.

I'm trying to create a named group of "outer" with the contents div#someId from div#someId input (in this case where someId matches .+)

However, Visual Studio is presenting the dialog: Grouped expression is missing ')'.

However, the ) is there after the +. What am I escapin开发者_开发知识库g or failing to escape?

EDIT:

I don't necessarily need to used named groupings, I just need to be able to use div#someId in the replace to add another selector.


It appears that Visual Studio's find and replace uses braces { } and not parentheses ( ) for backreferences and named references.


Since someone may find this trying to use backreferences/named grouping in a Find and Replace in Visual Studio, here's an example of using them:

To replace

div#someId input

with

div#someId input,
div#someId textarea

Find: {^div\#.+ }input and replace with: \0,\n\1textarea

It will put div#someId (with the space, which matches ^div\#.+ into the first backreference.

Building the replace string with a backreference:
The replace will then replace the whole string :\0

div#someId input

then a comma: ,

div#someId input,

then a new line: \n

div#someId input,
 

then the first group: \1

div#someId input,
div#someId 

then the given text: textarea

div#someId input,
div#someId textarea

Altogether makes the replace string \0,\n\1textarea

Update: Based on this MSDN question in VS2012 backreferences are accessed using $n rather than \n, so the replace string would be: $0,\n$1textarea


You should use the Productivity Power Tools' inline Replace feature, which uses standard .Net regexes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜