3

This is a small question, but it has been nagging me for a while. When I do a Find or Find/Replace in Eclipse and I use ^ or $ and check the Regular Expressions box, I always get a message:

String Not Found

I don't understand why. I know about the CTRL+SPACE content assist feature and even when I select ^ from the list, it does not work.

I was looking at this question, Regex to match start of line and end of line for quick find and replace, but it is not working for me. Other regex searches like ^. or ;$ work fine.

I see that ^ and $ are anchor characters, and do not match a string per se, but these characters seem to work in other editors.

EDIT:

For example, I want to convert a list of data into an SQL in clause.

From:

1
2
3
Hi
I need to quote these!

To:

'1',
'2',
'3',
'Hi',
'I need to quote these!',
4

2 回答 2

6

^ and $ are zero-width anchors as you said, therefore they will not match anything by themselves. You need to use something with the anchors in order to match anything in your file.

于 2013-03-06T19:41:47.140 回答
1

I was able to do it with this regex replace:

Find: ^(\s*)(.*)$

Replace: $1'$2',

So what I do is use:

  1. ^ to find the beginning of the line.
  2. (\s*) to find and capture any white space.
  3. (.*) to find and capture anything.
  4. $ to find the end of line.

Then replace with

  • $1 First capture
  • ' quote
  • $2 Second capture
  • ', quote comma

I wish ^ and $ worked by themselves, but this is cool b/c I can do it in one operation.

于 2013-03-06T21:20:06.027 回答