Ok perhaps ive bitten off too much here... I know you are not supposed to parse xml/html to regex but the thing is there just arent many other options. Im using AS3 im parsing the source of textflowlayout text to a different format.
string to parse :
< fontFamily=Verdana encoding=unicode fontWeight="bold"> some text < fontFamily=Verdana encoding=unicode > some text < fontFamily=Arial encoding=unicode fontStyle="italic"> some text < fontFamily=Arial encoding=unicode fontWeight="bold" fontStyle="italic"> some text
what i really need is:
< fontname=Verdanabold encoding=unicode> some text < fontname=Verdana encoding=unicode > some text < fontname=Arialitalic encoding=unicode > some text < fontname=Arialbolditalic encoding=unicode > some text
logically i think of it as taking apart the string into substrings checking if there are fontWeight or fontStyle in the substring if there is then appending the font name with the weight or style so so that the font name becomes font NameWeightStyle. then rebuilding the string. The font could be any font with variouse styles or weights.
Or please any other bright ways to do this will be appreciated.
so far:
pattern = /<(.*?)>/gixsm;
var matches:Object = pattern.exec(str);
var finalstring:String = "";
for each ( var i:String in matches ) {
if(i!='0'){
Alert.show(i);
pattern = /fontFamily=\"([^"]*)"/i;
if(pattern.test(i)==true){
pattern = /fontFamily=\"([^"]*)\" /i;
var resultfontFamily:Object = pattern.exec(i);
var fontFamily:String = resultfontFamily[1].toString();
/*pattern = /fontWeight=\".*?\" /i;
if(pattern.test(i)==true){
pattern = /fontWeight=\"([^"]*)\" /i;
开发者_运维技巧 var resultfontWeight:Object = pattern.exec(i);
var fontWeight:String = resultfontWeight[1].toString();
fontFamily = fontFamily+fontWeight;
}
pattern = /fontStyle=\"([^"]*)\" /i;
if(pattern.test(i)==true){
pattern = /fontStyle=\"([^"]*)\" /i;
var resultfontStyle:Object = pattern.exec(i);
var fontStyle:String = resultfontStyle[1].toString();
fontFamily = fontFamily+fontStyle;
}*/
fontFamily = "fontname="+fontFamily+" encoding=unicode";
pattern = /fontFamily=\".*?"/i;
finalstring += i.replace(pattern, fontFamily);
}
}
}
str = finalstring;
a few things 1 - the pattern to create the match object /<(.*?)>/gim; or /<([^<])+?>/gim; doesnt seem to work in as3 as an accurate gatherer of strings to array/object for this string???
2 assuming we have to put this sting back together - how would I access the "some text"?
any suggestions most welcome - much appreciated M
You're question is a bit ambiguous - i.e. you do not clearly specify if both the Style and Weight may both appear in a single tag. Also, you do not specify the whitespace and attribute value quoting requirements. That said, the following (javascript syntax) code should do the trick for just about any case:
p1 = /(<\s*fontname\s*=\s*)(['"]?)([^\s'"]+)(['"]?)([^>]*?)\s+fontWeight\s*=\s*['"]?([^\s'"]*)['"]?/ig;
p2 = /(<\s*fontname\s*=\s*)(['"]?)([^\s'"]+)(['"]?)([^>]*?)\s+fontStyle\s*=\s*['"]?([^\s'"]*)['"]?/ig;
str = str.replace(p1, "$1$2$3$6$4$5");
str = str.replace(p2, "$1$2$3$6$4$5");
Hopes this helps! :)
精彩评论