Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse Regex Search for beginning and end of line not working

Tags:

regex

eclipse

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!',
like image 775
Jess Avatar asked Sep 06 '25 03:09

Jess


2 Answers

^ 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.

like image 180
Philippe A Avatar answered Sep 07 '25 21:09

Philippe A


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.

like image 44
Jess Avatar answered Sep 07 '25 21:09

Jess