Pattern Checker - Test and learn string patterns in real time!

###Pattern Checker###

This is a simple project that allows you to enter a capture pattern for a string and display the results in real time.

  • Real time pattern checking
  • Support for multiple captures
  • List of each match returned
  • String highlighting
  • Link to Lua pattern documentation.

This is great for testing out your string patterns and it is a great tool to learn Lua string patterns!

Note: It’s still a little buggy on the text highlighting.

Code

@Briarfox - I tried it out with this
“pleaz give me the codes”
and it didn’t give me back the app I wanted :))

Seriously, though, real nice work, B-)

@briarfox awsome! The in-app link to doc is avery good idea too.
Maybe: add a list of commonnly used pattern examples?

Thanks @Ignatz and @Jmv38. I will be adding some commonly used patters as examples.

Nifty program/idea. Common patterns is a good idea, but how will you choose which ones are used enough? There are so many different patterns…

I’m not happy with the text output, the match print is very accurate but trying to color and format the string is proving to be harder then I thought

Very nice! A good tool to have on the App Store. (hint hint) :smiley:

@Zoyt I need to come up with a better way to color text and wrap it.

@Briarfox - if you’re playing with HTML, I may be able to help. What is the problem?

If you are interested in using the runtime and implementing a UITextBox and UIWebView for displaying it, I’d be happy to help (if you don’t know how to do it, I don’t know who knows what around here).

@Zoyt, I for one don’t know how to implement UITextBox and UIWebView in the runtime. Have been thinking about this for my project.

Is there a good guide you can post a link for? Or please could I ask/plead for a quick guide from yourself? :smiley:

I believe what Briarfox is looking for is a way to colour parts of a text string in Codea, in different colours, like this

Now this may not be what he wants, but I found a way to do it, if anyone is interested. It is difficult because you have to write each piece of different coloured text separately, and line it all up correctly, which is harder if the text is wrapping as well.

You can do this as follows. Assume you have a set of text strings, to be written one after the other, each in a different colour, as in the example above.

What you is write the entire string (ie all the strings joined together) in the colour of the last text string. So in the example above, you write “This is some colored text which is a mixture of different colors”, in black.

Then you write everything except the last string, in the colour of the next to last string. So we write “This is some colored text which is a mixture of different” in green. This overwrites everything except the last word “colors”, leaving it in black.

Then you write everything except the last 2 strings, ie "“This is some colored text which is a mixture of” in white. This leaves “different” in green and “colors” in black.

You work your way back to to the first string like this - the code below demonstrates.

function setup()
    --table of text and colours
    t={"This is some ","colored ","text ","which is a ","mixture ","of ","different ","colors",}
    c={color(0),color(255,0,0),color(0,0,255),color(0),color(255,255,0),color(255),
        color(0,255,0),color(0)}
    --create a table with combined strings
    tt={}
    tt[1]=t[1]
    for i=2,#t do
        tt[i]=tt[i-1]..t[i]
    end
end

function draw()
    background(150)
    textMode(CORNER)
    fontSize(24)
    textWrapWidth(300)
    print(w,h)
    textAlign(LEFT)
    --write the combined strings starting from the end and working back
    for i=#t,1,-1 do
        fill(c[i])
        --we need to adjust starting height
        --because Codea measures from the bottom of the text upwards
        local w,h=textSize(tt[i])
        text(tt[i],300,400-h)
    end
end

@Briarfox, When it matches a space, you can’t tell because nothing is drawn. I suggest putting a red underline or something in the space so you can tell it was matched.

@ignatz nice trick. Will not work with centered text though, but good enough.

@time_trial - I might post a guide to implementing add ons into Codea, but now promises. I’ve made many promises before that I don’t think I’ll stick to because I have so much going on in my life. We’ll see how much time I have after I finish StackIt v1.1.
And nice job @Ignatz!
Thanks!

@Ignatz, @Briarfox, here’s another way. @Ignatz’s shows some of the other colors under the proper one, this way doesn’t

-- Colored Text

-- Use this function to perform your initial setup
function setup()
    phrases = {
        { phrase = "This is text ", colour = color(226, 92, 9, 255) },
        { phrase = "with a few ", colour = color(0, 0, 255) },
        { phrase = "different colours ", colour = color(127, 0, 255) },
        { phrase = "and what not", colour = color(0, 127, 255) }
    }
end

-- This function gets called once every frame
function draw()
    background(255)
    
    fontSize(50) textWrapWidth(WIDTH) textAlign(LEFT)
    local fullPhrase = ""
    for _, p in ipairs(phrases) do
        fullPhrase = fullPhrase .. p.phrase
    end
    local fullW, fullH = textSize(fullPhrase)
    
    --[[fill(255,0,0)
    text(fullPhrase, WIDTH/2, HEIGHT/2)--]]
    
    local x, y = WIDTH/2, HEIGHT/2
    local prevW = 0
    for id, p in ipairs(phrases) do
        if p.row == nil then p.row = 0 end
        local curW, curH = textSize(p.phrase)
        local dx = (x - (fullW / 2) + prevW + (curW / 2)) - (p.row * fullW)
        local dy = y + (fullH / 2) - (curH * (p.row + 0.5))
        
        if dx + (curW / 2) > textWrapWidth() and not p.wrapped and p.phrase ~= " " then
            local nextPhrase
            if p.phrase:find("%s") then
                local a, b = p.phrase:match("([%l%p%d]*)%s(.*)")
                p.phrase = a
                nextPhrase = " " .. b
            else
                nextPhrase = p.phrase:sub(1, p.phrase:len())
                p.phrase = " "
            end
            table.insert(phrases, id + 1, { phrase = nextPhrase, colour = p.colour })
            for i = id+1, #phrases do
                phrases[i].row = p.row + 1 
            end
            p.wrapped = true
            
            local curW, curH = textSize(p.phrase)
            local dx = x - (fullW / 2) + prevW + (curW / 2)
        end
        fill(p.colour)
        text(p.phrase, dx, dy)
        
        prevW = prevW + curW
    end
end

@Zoyt, no worries. If you have the time, then it would be appreciated, but no pressure.

Interesting to see the different approaches to this. I was expecting to use

blendMode(ADDITIVE)

and place a coloured rectangle over the letters to alter the colour. It only seemed to blend on alpha though.

Here is something I had sitting around but didn’t have a use for it. Use ¥ as a control character followed by a letter for an offset into the color table. It would be easy to setup 26 colors corresponding to the letters a…z . Just put ¥ and a letter before the character you want to change color. This is also setup to pick an x,y screen position and a width to print.


displayMode(FULLSCREEN)

function setup()
    textMode(CORNER)
    str="¥aThis ¥cis ¥ddiff¥aerent ¥ccolored ¥dtext. You ¥bcan setup ¥aas many different ¥dcolors as you want. You can pick ¥athe screen ¥dx,y position and ¥ethe width to print. The colors are ¥ajust a matter ¥dof creating ¥ba ¥dlarger table of ¥ediff¥berent colors ¥dand adding ¥fthe control ¥bcharacters."
    col={a=color(255,0,0),
         b=color(0,255,0),
         c=color(0,0,255),
         d=color(255,255,0),
         e=color(0, 238, 255, 255),
         f=color(255, 0, 223, 255),
        }
end

function draw()
    background(40, 40, 50)
    showText(100,200,400)
    showText(200,600,150)
    showText(650,650,60)
end

function showText(x,y,size)   -- x,y screen position and width
    local count, xoffset, yoffset=0,0,0
    for z=1,str:len() do
        local b1,b2=str:byte(z,z+1)
        if count==0 then
            local ch=str:sub(z,z)
            if b1==194 and b2==165 then
                fill(col[str:sub(z+2,z+2)])
                count=2
            else
                text(ch,x+xoffset,y-yoffset)
                local w,h=textSize(ch)
                xoffset = xoffset + w
                if xoffset>size then
                    xoffset=0
                    yoffset = yoffset + h
                end
                count=0
            end
        else
            count=count-1
        end
    end
end

Last off topic post, but @time_trial - There’s a good chance I’m going to do the book. I have a week in Mexico where I won’t have my computer, so I’ll write it during that time.
Thanks!

So why does displaying text over other text that is a different color bleed the color and any way to fix it? I’m guessing this is the same thing that happens if no background is used.

Example:

function setup()
    str = "Hello World"
    x,y = WIDTH/2,HEIGHT/2
    
end

function draw()
    background(255, 255, 255, 255)
    fontSize(70)
    w,h = textSize(str)
    h = h/2
    for i=1,100 do
        fill(255, 0, 0, 255)
    text(str,x,y+h)
        fill(195, 195, 195, 255)
    text(str,x,y+h)
    end
    text(str,x,y-h)
end