String replacing with Regex

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

String replacing with Regex

Post by MaxV » Wed Dec 18, 2013 1:03 pm

Hello,
I read on forum and search engines, but I didn't find a solution.
I need to substitute in a text like this:

Code: Select all

# Title 1
Some text, very long...
# Title 2
Some other text, very long...
...
to html like this:

Code: Select all

<h1># Title 1</h1>
Some text, very long...
<h1>#Title 2</h1>
Some other text, very long...
...
I can't use replace, since I have to store the content between # and newline.
I can't use neither matchText(), because I don't know how many replace I need.
I only know that a good regex for my seach is:

Code: Select all

(\n|^)#.*\n
Do you have any idea?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String replacing with Regex

Post by Klaus » Wed Dec 18, 2013 1:54 pm

Hi Max,

I have no idea about RegEx, so I would do something like ntis:

Code: Select all

...
## tRawText will contain your list WITHOUT htmltags
## tHTMLText = your list with htmltags
put empty into tHTMLText
repeat for each line tLine in tRawText
  if char 1 of tLine = "#" then
    put "<h1>" & tLine & "</h1>" & CR after tHTMLText
  else
   ## You might want to add <p></p> tags here ;-)
   put tLine & CR after tHTMLText
 end if
end repeat
delete char -1 of tHTMLList
## Now do whatever you like with tHTMText
...
:D


Best

Klaus

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: String replacing with Regex

Post by MaxV » Wed Dec 18, 2013 3:08 pm

Thank you Klaus, I'll try to do all my tasks with chunk expressions...
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: String replacing with Regex

Post by Klaus » Wed Dec 18, 2013 3:33 pm

Oops, little typo in the last 2 lines:
...
delete char -1 of tHTMLText ## not tHTMLList!
## Now do whatever you like with tHTMLText
...

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: String replacing with Regex

Post by MaxV » Thu Dec 19, 2013 5:00 pm

Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply