Re: new for regular expression in Perl



Hi Shashi,

Thanks for the reply.

Sorry I didn't make myself clear enough in the
previous email. If I read the whole file into an array
(@file) and then change it into a scalar($string) the
position of each word will change from the second
line. If I want to know the position of each match the
return position will be different from those in the
original file. This is what I mean " a little bit
unnatural for me".(You are correct that the original
file never changes unless you write something to it).
I think it might be natural for me to read the file
line by line and get the return position looks like
these(just an example), similar to do the word search
in microsoft Word, which is what I really want:

match in line 1 and the end of matching position is 4

match in line 4 and the end of matching position is 24

.......


--- Shashidhara Bapat <shashidharasbapat@xxxxxxxxx>
wrote:

> Hi Chen,
>
> You can do one line at a time also.
>
> (Also, if you read whole file, convert it into a
> string and work on that
> string, the original file will not change.)
>
> - Regards,
> Shashi.
>
>
> On 1/4/06, chen li <chen_li3@xxxxxxxxx> wrote:
> >
> > Thanks Chris and others for the information.
> >
> > Chris, I have another question: I have a file
> > containing multiple lines and it looks like this:
> >
> > (line 1).....chen.....
> > (line 2)..............
> > (line 3) chen.........
> >
> > If I read the whole file at once and change it
> into a
> > string I have no problem using regular expression
> to
> > find out the word "chen". But it looks like a
> little
> > bit unnatural for me because it changes the file's
> > format. Is it possible to do the match without
> change
> > the file format? One way I think is to use a loop
> to
> > read the file line by line and do the match for
> each
> > line. I wonder if this is the best way to get the
> job
> > done.
> >
> > Once again thank you very much,
> >
> > Li
> >
> >
> > --- Chris Charley <charley@xxxxxxxxxxxx> wrote:
> >
> > >
> > > ----- Original Message -----
> > > From: "chen li" <chen_li3@xxxxxxxxx>
> > >
> > >
> > > > Hi all,
> > > >
> > > > Here is my problem:
> > > >
> > > > my $string="chen schen";
> > > >
> > > > I want to use regular expression to find the
> exact
> > > > match in the string. So when I want to match
> > > "chen" I
> > > > expect "chen" only.
> > > > But use the following line I get both "chen"
> and
> > > > "schen" at the same time.
> > > > $string=~/chen/g;
> > > >
> > > > How do I get what I expect?
> > >
> > > Hi Chen
> > >
> > > You can get the results by adding a \b before
> and
> > > after your reg expression.
> > > \b is a boundary between a word and a non-word
> > > character. (A word character
> > > is a-z, A-Z, 0-9, or underscore, _).So, for your
> > > example, schen wouldn't
> > > match then because the 's' preceding 'c' is a
> word
> > > character and so the \b
> > > wouldn't be true. But, it would match chen
> because
> > > the (non) character
> > > (beginning of the string) preceding the 'c'
> would
> > > make \b true.
> > >
> > > $string=~/\bchen\b/g;
> > >
> > > MATCH
> > > "chen "
> > > "#chen"
> > > "here is a chen and another chen"
> > > "chen's" (the apostrophy is a non-word char)
> > >
> > > NO MATCH
> > > "schen"
> > > "chens"
> > >
> > > Chris
> > >
> > >
> > >
> > > --
> > > To unsubscribe, e-mail:
> > > beginners-unsubscribe@xxxxxxxx
> > > For additional commands, e-mail:
> > > beginners-help@xxxxxxxx
> > > <http://learn.perl.org/>
> > > <http://learn.perl.org/first-response>
> > >
> > >
> > >
> >
> >
> >
> >
> > __________________________________________
> > Yahoo! DSL ? Something to write home about.
> > Just $16.99/mo. or less.
> > dsl.yahoo.com
> >
> >
> > --
> > To unsubscribe, e-mail:
> beginners-unsubscribe@xxxxxxxx
> > For additional commands, e-mail:
> beginners-help@xxxxxxxx
> > <http://learn.perl.org/>
> <http://learn.perl.org/first-response>
> >
> >
> >
>




__________________________________________
Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com

.



Relevant Pages

  • Re: simple binding of controls to object properties - refresh cont
    ... Thanks again Chris for looking into this.! ... Still I think I prefer just manually reseting the bindings, ... public string Ting ... textbox BobTheBound = new TextBox; ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Programmers unpaid overtime.
    ... > problem in this reasoning, ... > Chris Sonnack presented some interesting numbers. ... Over and above the individual cache for a string ... > a PowerString instance and its string. ...
    (comp.programming)
  • Re: Variable formula creation in vb.net
    ... Sure Chris. ... Remember that the expression is going to be a string. ... the business rules for creating your formulas, ... >> Chris Davoli ...
    (microsoft.public.dotnet.framework)
  • RE: Deleting recors in a linked table
    ... your original file. ... Function deleteLinesInTextFileForQuinto(file As String, criterium As String) ... Dim tempFile As String ... Const ForReading = 1 ...
    (microsoft.public.access.queries)
  • Re: partial file name string search
    ... Chris wrote: ... even using a doss driven exe which takes a partial string ... You can also use the search feature built into Explorer. ...
    (microsoft.public.win2000.general)