[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regular expression question...

hongseok.yoon

5/26/2008 8:42:00 AM

I'd like to make sort of a comment remover for C++.

If there's a source file like bellow...

<source.txt>
// 1234
12//34
///1234
12///34

and my code is...

file = open("source.txt", "r")
file.each_line {|line|
if line.match /(.*)\/\//
puts $1 if $1.length > 0
end
}
file.close

result is...

12
/
12/

but!, what I expected is~

12
12

What's wrong with this code...(I think I may not understand regular
expression totally...) and what should I do for this?
3 Answers

Sebastian Hungerecker

5/26/2008 8:49:00 AM

0

hongseok.yoon@gmail.com wrote:
> file =3D open("source.txt", "r")
> file.each_line {|line|
> =A0 =A0if line.match /(.*)\/\//
> =A0 =A0 =A0 puts $1 if $1.length > 0
> =A0 =A0end
> }
> file.close
>
> result is...
>
> 12
> =A0 /
> 12/
>
> but!, what I expected is~
>
> 12
> 12

=2E* tries to match as much as possible (greedy) so the \/\/ will match the=
=20
last // it finds - not the first. If you want to make the .* non-greedy, ad=
d=20
a ? after the *.

HTH,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

Robert Klemme

5/26/2008 9:49:00 AM

0

2008/5/26 <hongseok.yoon@gmail.com>:
> I'd like to make sort of a comment remover for C++.
>
> If there's a source file like bellow...
>
> <source.txt>
> // 1234
> 12//34
> ///1234
> 12///34
>
> and my code is...
>
> file = open("source.txt", "r")
> file.each_line {|line|
> if line.match /(.*)\/\//
> puts $1 if $1.length > 0
> end
> }
> file.close
>
> result is...
>
> 12
> /
> 12/
>
> but!, what I expected is~
>
> 12
> 12
>
> What's wrong with this code...(I think I may not understand regular
> expression totally...) and what should I do for this?

Additional remarks: using the block form of File.open is usually
better because code will be more robust. Here's another solution:

File.foreach "source.txt" do |line|
puts line.sub(%r{//.*}, '')
end

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

John

5/13/2009 8:04:00 AM

0

this worked for me.

With ActiveWorkbook.Sheets("MarginAnalysis")
.Range("b225:b270").Sort Key1:=.Range("b225"), _
Order1:=xlDescending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End With
--
jb


"bigjim" wrote:

> OK, I tried this code:
>
> With ActiveWorkbook.Sheets("MarginAnalysis").Range("b225:b270").Select
> Selection.Sort Key1:=Range("b225"), Order1:=xlDescending, Header:=xlNo, _
> OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
> DataOption1:=xlSortNormal
>
> End With
>
> Now I'm getting this error:
> "Sort reference is not valid. Make sure it's witing the data ou want to
> sort and the first sort by box isn't the same or blank.
>
> I'm not sure what that is saying.
>
> Jim
>
> "Jim Cone" wrote:
>
> > You are telling Excel to sort the entire sheet in this portion of the code...
> > Sheets("MarginAnalysis").Cells
> >
> > The "Key1:=.Range("b225:B270")" portion specifies the column to sort by.
> >
> > Change .Cells to .Selection if you want to sort the selection.
> > However, the selection must intersect with column B.
> > --
> > Jim Cone
> > Portland, Oregon USA
> >
> >
> >
> > "bigjim"
> > <bigjim@discussions.microsoft.com>
> > wrote in message
> > I'm using excel 2003. I want to sort data formatted as text in a column.
> > Most of the cells in the column are blank. I am using the following code:
> >
> > With Sheets("MarginAnalysis")
> > ..Cells.Sort Key1:=.Range("b225:B270"), _
> > Order1:=xlAscending, Header:=xlNo, _
> > MatchCase:=False, Orientation:=xlTopToBottom
> > End With
> >
> > The error I get is "This operation requires the merged cells to be
> > identically sized. " There are no merged cells anywhere in the range
> > selected. When I go to the worksheet "marginanalysis" after the program
> > stops the range b225:b282 is highlighted.??
> > I'm lost and any help would be appreciated.
> >
> > Jim
> >