views:

17749

answers:

11

So I've got a big text file which looks like the following:

<option value value='1' >A
<option value value='2' >B
<option value value='3' >C
<option value value='4' >D

It's several hundred lines long and I really don't want to do it manually. The expression that I'm trying to use is:

<option value='.{1,}' >

Which is working as intended when i run it through several online regular expression testers. I basically want to remove everything before A, B, C, etc. The issue is when I try to use that expression in Vim and Notepad++, it can't seem to find anything.

Thank you.

+5  A: 

This will remove the option tag and just leave the letters in vim:

:%s/<option.*>//g
xsl
+2  A: 

It may help if you're less specific. Your expression there is "greedy", which may be interpreted different ways by different programs. Try this in vim:

%s/^<[^>]+>//
Lucas Oman
The '+' needs to be escaped: ^<[^>]\+> for this expression to work. I like this version better than the @xsl answer that is getting voted up.
Sixto Saez
Shoot, you're right, thanks for the correction. And thanks; I always believe simpler is better, esp. when it comes to regex.
Lucas Oman
+1  A: 

This will work. Tested it in my vim. the single quotes are the trouble.

1,$s/^<option value value=['].['] >/
J.J.
+1  A: 

Vim:

:%s/.* >//

akr
+5  A: 

There are two problems with your original solution. Firstly, your example text:

<option value value='1' >A

has two occurences of the "value" word. Your regex does not. Also, you need to escape the opening brace in the quantifier of your regex or Vim will interpret it as a literal brace. This regex works:

:%s/<option value value='.\{1,}' >//g
rmeador
+1  A: 

There is a very simple solution to this unless I have not understood the problem. The following regular expression:

(.)(>)(.)

will match the pattern specified in your post.

So, in notepad++ you find (.)(>)(.) and replace it with \3.

The regular expressions are basically greedy in the sense that if you specify (.*) it will match the whole line and what you want to do is break it down somehow so that you can extract the string you want to keep. Here, I have done exactly the same and it works fine in Notepad++ and Editplus3.

andHapp
+2  A: 

In vim

:%s/<option value='.\{1,}' >//

or

:%s/<option value='.\+' >//

In vim regular expressions you have to escape the one-or-more symbol, capturing parentheses, the bounded number curly braces and some others.

See :help /magic to see which special characters need to be escaped (and how to change that).

rampion
+3  A: 

Everything before the A, B, C, etc.

That seems so simple I must be misinterpreting you. It's just

:%s/<.*>//
Whaledawg
In vim, that is.
Whaledawg
+1  A: 

Thanks for everyones replies.

Learned a bit more about regular expressions.

Mike
+2  A: 

In Notepad++ :

<option value value='1' >A
<option value value='2' >B
<option value value='3' >C
<option value value='4' >D


Find what: (.*)(>)(.)
Replace with: \3

Replace All


A
B
C
D
+1  A: 

Having the same problem (with jQuery " done..." strings), but only in Notepad++, I asked, received good friendly replies (that made me understand what I had missed), then spent the time to build a detailed step-by-step explanation, see Finding Line Beginning using Regular expression in Notepad++

Versailles, Tue 27 Apr 2010 22:53:25 +0200

Michel Merlin