Problems Applying Stylesheet Instead of Local Formatting

This forum is for all Flare issues related to styles, stylesheets and XML.
RamonS
Senior Propellus Maximus
Posts: 4293
Joined: Thu Feb 02, 2006 9:29 am
Location: The Electric City

Re: Problems Applying Stylesheet Instead of Local Formatting

Post by RamonS »

They may differ based on the symbols reserved by the various languages. Basically, it is an expression that defines a pattern, from which one or more parts can be defined as variable and also be restricted as to which content they may include. The function principle is the same anywhere. I found a tutorial for reg exp here:
http://www.mkssoftware.com/docs/man5/regexp.5.asp
Alice
Propeller Head
Posts: 31
Joined: Wed Dec 31, 2008 4:58 am
Location: Belarus

Re: Problems Applying Stylesheet Instead of Local Formatting

Post by Alice »

Hi Kevin!
In fact i'm aware of the setting you are talking about. it works fine in V4.1 (I'm already using it), but in V3.1 it seemed to be searching the whole project but didn't show the results. It showed neither Find Results 1 nor Find Results 2. That's why I said that it was poor.
But thanks for the reply :wink:
Alice
NorthEast
Master Propellus Maximus
Posts: 6365
Joined: Mon Mar 05, 2007 8:33 am

Re: Problems Applying Stylesheet Instead of Local Formatting

Post by NorthEast »

Alice wrote:So does anybody know some quick and easy way to get the idea and the basics of those FRIGHTENING regular expressions?
When I had a look I had a look a while back, I bookmarked these two...
http://www.regular-expressions.info/
http://zytrax.com/tech/web/regex.htm
It looks pretty powerful, but I couldn't quite get my head round it.
Alice wrote:In fact i'm aware of the setting you are talking about. it works fine in V4.1 (I'm already using it), but in V3.1 it seemed to be searching the whole project but didn't show the results. It showed neither Find Results 1 nor Find Results 2.
I think there's a mix-up here, Flare has two 'finds' - a find and replace and a find in files. The find and replace pane is where you'd do a find and replace using regular expressions, and it has a setting to find in the whole project (which worked fine in v3.1).
The find in files pane is just for finding something in your files (with no replace), and it puts the list of matches in the Find Results 1 or 2 panes. The Find Results 1 and 2 panes are only used with the find in files, not find and replace.
Alice
Propeller Head
Posts: 31
Joined: Wed Dec 31, 2008 4:58 am
Location: Belarus

Re: Problems Applying Stylesheet Instead of Local Formatting

Post by Alice »

Dave Lee wrote:I think there's a mix-up here, Flare has two 'finds' - a find and replace and a find in files.
in v3.1 in find in files flare didn't display neither of the results panes, since i couldn't find and look through cirtain things, i didn't dare to run find and replace. so i was just careless with wording.
In fact i didn't spend much time solving the problem(i tried-> it didn't work out->i dropped it). It's only now that i started active editting, projects restructing and cleaning up (due to the fact that after lot's of work i'll aply GPL and will save myself lots of effort). And all the stuff started because i want to single source the stylesheet.
thanks a lot. I've already been to http://www.regular-expressions.info and it seems nice, I'll look through the other one. In addition i discovered some links in my native language. Although i'm a certified specialist in English such important things shoud be studied in both languages :D
Thanks again
Alice
NorthEast
Master Propellus Maximus
Posts: 6365
Joined: Mon Mar 05, 2007 8:33 am

Re: Problems Applying Stylesheet Instead of Local Formatting

Post by NorthEast »

Good luck. I think most of the stuff I found about regular expressions was written in geek rather than English!
Alice
Propeller Head
Posts: 31
Joined: Wed Dec 31, 2008 4:58 am
Location: Belarus

Re: Problems Applying Stylesheet Instead of Local Formatting

Post by Alice »

hurray! :D
I made my first regular expression and it was sucsessful!
Alice
iand
Sr. Propeller Head
Posts: 131
Joined: Thu Dec 18, 2008 5:46 am
Location: London, England

Re: Problems Applying Stylesheet Instead of Local Formatting

Post by iand »

Hi, glad the regular expression was helpful. Useful to know that you can use these in Flare. They are definitely tricky but quite powerful and probably reward the effort needed to learn them although maybe it is easier to use a tool like FAR. Anyway, here is an attempt at an explanation as to how the following regular expression works:

Search Expression:
img src="([^"]+)[^>]+

Replace Expression:
img src="\1" class="Centre" /

[^"]+ matches as many characters as possible until it hits this character " - this captures the image filename and path.
Putting it in brackets ([^"]+) means, remember what we matched here - this is called a backreference.

[^>]+ matches as many characters as possible until you hit this character >

Example: <img src="C:/test/image.jpg" style="abcde"/>

[ab] is called a character set in the jargon of regular expressions; it matches 1 character either a or b
[^a] matches 1 character as long as it is not a

to match more than one character you need to put a + after the character set. This is a "greedy" match it tries to match as any many characters as possible. I think there is a "lazy" match but I have never used it.

[^"]+ matches C:/test/image.jpg
[^>]+ matches style="abcde"/
In total we match img src="C:/test/image.jpg" style="abcde"/

\1 is the contents of the 1st backreference in the search expression, so whatever matched that part of the search expression. If we used a second backreference you would use \2, etc.

As mentioned before in total in the example we match img src="C:/test/image.jpg" style="abcde"/
"\1" class="Centre" / in the replace expression is "C:/test/image.jpg" class="Centre" / in this example
So we replace img src="C:/test/image.jpg" style="abcde"/ with "C:/test/image.jpg" class="Centre" / ....Phew!

My advice on regular expressions is:

1. Cheat and copy ones other people have made already!

2. If you can't do 1 then build a regular expression up in pieces and test each piece works before making the expression more complicated. Make sure you get the search part perfect and that you are not matching anything you don't expect.

3. Learn what the special characters are (metacharacters in the jargon) these are +()*?\ etc. if you want to match these in text then you must put a \ in front of them like so \( to match ( This is called "escaping". This is really annoying! Especially if you want to match \ then you need to put \\. This is very easy to get wrong! Sometimes . is a metacharacter as well that matches any character (as people have mentioned they do vary a bit in different applications).

4. Learn character sets [] \d \s \w + and . backreferences and ().

5. Use lazy evaluators rather than greedy ones: +? and *? rather than + and *

6. Use negative character sets where you can e.g. <img[^>]+> to match an img tag

That should get you started.

I second the recommendation for this site: http://www.regular-expressions.info/tutorial.html

Cheers, Ian
Last edited by iand on Tue Mar 31, 2009 4:51 am, edited 1 time in total.
iand
Sr. Propeller Head
Posts: 131
Joined: Thu Dec 18, 2008 5:46 am
Location: London, England

Re: Problems Applying Stylesheet Instead of Local Formatting

Post by iand »

Unfortunately my free trial of Powergrep has expired and I have had to fall back on Flare's regular expression feature. I hit a problem here, Flare doesn't support the use of backreferences which rules out many applications of regular expressions such as putting every <img ..../> tag inside a div tag in all your files.

Backreferences allow you to use what you match with your regular expression in replacement text.
I encourage everyone who uses regular expressions in Flare to submit a bug request for this at the usual place: http://www.madcapsoftware.com/bugs/submit.aspx
Post Reply