Like this:
.,$ s/<(\d+)>/changed \1 /
Main Topics
Browse All TopicsI am trying to pull out the value that is contained between the greater than and less than operator but I must use regular expression.
Example <value>
Example below I need to pull lines with <8>, <9> and <10> but my regular expression below does not work.
Regular Expression:
.,$ s/\(.*\)<\([8-9]|[10]\)>\(
line number <7>
line number <8>
line number <9>
line number <10>
line number <11>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The comments above may be all you need. I am actually a little confused by what you need from the expression and what the "text" is like.
I do have one thing to point out or ask. It seems like you want to capture the number used in between the less than and greater than sign (e.g 8 from <8>). If that is true then you shouldn't be escaping the signs in your expression. For example instead of ...
<\([8-9]|[10]\)>
you would have ...
<([8-9]|[10])>
That will look for the signs and either 8, 9 or 10 in between. The number will be placed in a group for you to use later. Unless you expression engine or language requires the parentheses to be escaped the escape will cause a problem.
I hope that helps. Let me know if you have a question about this. If you need more help then please clarify what the "text" is like and what exactly you need.
bol
@Zvonko - THANKS! I actually used it from the question and thought something looked off. I just couldn't identify the problem when I posted my comment last night. You are right about [10] not working for the number 10 because it would match 1 or 0. However the hypen, even in square brackets, is treated as a special character. I believe the Asker wanted to match the range of numbers from 8 to 9 (i.e. 8 or 9) and [8-9] would do that. If the intent was to match those three characters literally and in that order then you are correct, the square brackets wouldn't work. Thanks for your post to correct the error I overlooked. I can't believe I missed that. :)
bol
Thanks for all the response and sorry for the delay on my response.
Basically what I need is lines that have single and double digits between the < and > sign. So the two expressions work below:
This expression:
1,$ s/\(.*\)<[89]>\(.*\)/chang
Matches on
line number <8>
line number <9>
This expression:
1$ s/\(.*\)<[1][0]>\(.*\)/cha
Matches on
line number <10>
So I need [89] or [1][01]
I cannot get all three lines out with one regular expression to match on
line number <8>
line number <9>
line number <10>
Did you try Zvonko's suggestion in http:#19601617. That will look for 8, 9 or 10.
You are using square brackets too much and wrong. For example you don't need to ever use them if you only have one character inside (e.g. [1] should be just 1). The comments above have some instructions on making an expression that could be useful to you. Let us know if you have a question on what we said. Also let us know the result of using Zvonko's expression.
bol
I can only assume that you want this single regular expression to be expandable to take a list of numbers other then just 8, 9 and 10.
In that case you can literally put the list of numbers that you want in the regular expression! Here is what mine looked like for 8, 9, and 10:
s/(.*?)<(8|9|10)>(.*?)/cha
Notice that inside <( ... )> is just a pipe delimited list of numbers. Its easy to create a such a string from a list (an array) of numbers. Just join each element of the list with a |. Here are examples in perl and ruby:
perl:
@arr = qw/ 11 2 3 /;
$str = join '|', @arr;
print $str, "\n"
ruby:
puts [11,2,3].join('|');
Each results in:
11|2|3
And each could easily be put into the regular expression in the specified spot.
- Joe P
This is the regular expression I had to use in vi... I want to point out that vi is not very nice when it comes to regular expressions because you have to escape common characters like (, ), and |.
In VI go into command mode, and type the following (including the colon to get into that mode)...
:g/^\(.*\)<\(8\|9\|10\)>$/
The first half identifies all lines (g = global) that match the regular expression:
g/^\(.*\)<\(8\|9\|10\)>$/
The second half then runs the s/// search and replace on that line... using the same regex:
s/^\(.*\)<\(8\|9\|10\)>$/c
Maybe someone knows a better way for this to work but I couldn't seem to get the search and replace to work on more than one line unless I did the two commands. Hope this helps... all of the above regular expressions work, just not in VI because regex in VI take extra measures....
- Joe P
Well... I looked at the questions again and I noticed your example uses a cool trick with ranges. .,$! So everything can be done like so:
:.,$ s/\(.*\)<\([8-9]\|10\)>\(.
If your example you needed to escape the pipe => \|
And if you wanted 10 then you needed to remove the brackets => 10
And the thing with 8-9 has been mentioned above.
Good thinking,
Joe P
Business Accounts
Answer for Membership
by: DarkoLordPosted on 2007-07-30 at 15:02:17ID: 19596081
What about something like this:
<(.*)>