lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>    I was looking for a multi line string search for a specific pattern,
> Following is an example :
>
>    local str="this is sample text with lots of symbols [test]<test>*test*
> and it's multiline too [10,000 words]"
>    local pattern=<test>
>

Here are a few tips but you really must read the reference manual,
there are so many subtleties involved.  The Lua Users' Wiki is
also useful:
http://lua-users.org/wiki/StringTutorial
http://lua-users.org/wiki/StringRecipes

1. The newline character is a byte like any other.
2. Some systems end a line you type in with only a newline,
    others insert another character too.  If you read in a file
    using file:lines(), you don't need to worry about this.
3. A string in your program containing actual newlines must be
    enclosed in [[…]], not  "… ".
4. Or you can code it as "This line\nNext line".  The backslash
    escapes the letter n so the combination means newline.
5. Search patterns follow a different rule.  The percent sign
    is a special escape character.  Certain characters are _magic_,
    i.e. they mean something different inside a pattern and need
    to be escaped (preceded by a percent) if you mean just the
    character.  It's always safe to do that if you can't remember.
    So the pattern "%<test%>" will certain match "---<test>--".
    Would "<test>" also match it?  The manual will say …

But maybe you know all that, your problem is not to find the
substring but to describe its position as character this on line
that.  That's quite tricky.  Read the Wiki articles.