I have both the "5.1 Reference Manual" and the "Programming in Lua: 3rd Ed." in front of me. Reading these, as well as numerous searches on the web, still leave me a bit confused when it comes to using string.match and string.gmatch.
I understand that they both are used to locate patterns.
Here are an example they use in the "Reference Manual" for string.gmatch:
s = "hello world from Lua"
for w in string.gmatch (s, "%a+") do
print(w)
end
I understand that this will iterate over all of the words in s and print them each one per line.
Here is an example they use in the "Programming in Lua" book for string.match:
date = "Today is 17/7/1990"
d = string.match(date, "%d+/%d+/%d+")
print(d) -- prints 17/7/1990
What I'm confused about is when is it appropriate to use one over the other?
For example, you had code you wanted to parse that had contained the same pattern dozens of times throughout it. This pattern contained variables you needed, which would be a better choice? Example code below (x's are all variable data that differs from the other lines. data can be any garbage you didn't care about looking for and it was all just noise):
Header contains variable (HERE) and (HERE) I want.
data data data data data data data data
<Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX>
data data data data data data data
<Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX>
<Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX>
<Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX>
data data data data data data data data data
<Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX>
<Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX>
data data data data data data data data data data data data data data data data data data data data data data data data
<Font Typeset:xxxx Font Color:xxx Font Xpos:xxx Font Ypos:xxx Font Bold:X Font Uline:X Font Italic:X Font Text:XXXXXXXXX>
Footer here also has three variables I want (here)/(here) and (here)
This code obviously has a pattern to it. But, if wanted to create a simple function that parsed the data and grabbed the variables, which is the better choice?
function match(data)
local f_type, f_color, f_xpos, f_ypos, f_bold, f_uline, f_italic, f_txt = data:match("<Font Typeset:(.-) Font Color:(.-) Font Xpos:(.-) Font Ypos:(.-) Font Bold:(.-) Font Uline:(.-) Font Italic:(.-) Font Text:(.-)>
print(f_type, f_color, f_xpos, f_ypos, f_bold, f_uline, f_italic, f_txt)
end
...or...
function gmatch(data)
local f_type, f_color, f_xpos, f_ypos, f_bold, f_uline, f_italic, f_txt = data:gmatch("<Font Typeset:(.-) Font Color:(.-) Font Xpos:(.-) Font Ypos:(.-) Font Bold:(.-) Font Uline:(.-) Font Italic:(.-) Font Text:(.-)>
print(f_type, f_color, f_xpos, f_ypos, f_bold, f_uline, f_italic, f_txt)
end
Does gmatch just iterate over the entire code (data in this example) and return all instances where the pattern is true where match only does the first?
In what scenarios is one better than the other?
ETA: I added a header and footer to the example code. This header and footer both contain variables I want to use. Now this entire chunk of code (header/body/footer) are repeated numerous times throughout the same input file that I want to parse. So there are patterns within patterns.
Does gmatch just iterate over the entire code (data in this example) and return all instances where the pattern is true where match only does the first?
It returns an iterator for doing so.
Returns an iterator function that, each time it is called, returns the next captures from pattern (see ยง6.4.1) over the string s. If pattern specifies no captures, then the whole match is produced in each call.
In what scenarios is one better than the other?
string.gsub is best when you need to substitute the matches without regard to their position.
string.gmatch is best when you are only interested in the matches, and want to iterate over them.
string.match gives you all the captures from the first match.
string.find is the most versatile, returning the first match and its position. The cost is not being specialized for any task, thus needing more code.
Related
Is there a way to get a proper Font instance out of a tag, so I can change just the size? I know I can dump it in tk.font.Font but that seems really inefficient.
for tag in self.tag_names():
#I need `f` to be a Font instance, not just a string
f = self.tag_cget(tag, 'font')
If you used font objects to create the tags, then you can get the font name from the tag and then use nametofont to convert it to an instance of tkinter.font.Font. However, this only works if the tag has a font associated with it, and if the font is a font object rather than shorthand notation (eg: ("Helvetica", 24, "bold")).
from tkinter.font import nametofont
...
for tag_name in self.tag_names():
font_name = self.tag_cget(tag_name, "font")
if font_name:
font = nametofont(font_name)
size = int(font.cget("size"))
font.configure(size = size + delta)
One method is to premake all of the Font instances and assign the instances to the applicable tag's font option. When you want to change the sizes, loop over the stored Font instances, and change their sizes directly. Every tag that uses your Font instances will change accordingly.
in before xml in TextView we have
holder.binding.iconInfo.text = "&#x${iconValue}"
and set out icon font to it with typeface dose anyone can help same thing but with jetpack compose
The specific syntax you mention that can be used with TextViews is due to the fact that XML is used when defining layouts. That is an XML numeric character entity that specifies a character with its unicode value in hexadecimal notation.
So it is not related to TextViews, layouts, fonts or icons.
If you want to do the same with a String in Kotlin and the character value is dynamic, you can do something like this
val text = "${Char(iconValue)}"
In Compose a custom font can be defined either as part of your Theme -> Typography and then used as text style or you can define it directly like this
val fontFamily = FontFamily(
// If you have the font in resources
Font(R.font.my_font_normal, weight = FontWeight.Normal),
Font(R.font.my_font_bold, weight = FontWeight.Bold),
Font(R.font.my_font_italic, weight = FontWeight.Normal, style = FontStyle.Italic),
// If you are loading the font at runtime from a file
Font(File(...), weight = FontWeight.Normal),
// ...
)
// Use with Text composable
Text(text = text, fontFamily = fontFamily)
I want to read ppt and highlight certain word(background color added if background color is not possible then change the font color) in the slide. I am able to do it at sentence level i.e. if that word is contain in paragraph then it will change the font color of whole text but i want to change the color of that word only.
How about the following for highlighting?
def set_highlight(run, color):
# get run properties
rPr = run._r.get_or_add_rPr()
# Create highlight element
hl = OxmlElement("a:highlight")
# Create specify RGB Colour element with color specified
srgbClr = OxmlElement("a:srgbClr")
setattr(srgbClr, "val", color)
# Add colour specification to highlight element
hl.append(srgbClr)
# Add highlight element to run properties
rPr.append(hl)
return run
It's hacking the XML but for highlighting (text background) that's what you have to do.
I am trying to copy the excel content to PPT. using this command
`pptSlide.Shapes.PasteSpecial DataType:=ppPasteHTML, Link:=msoFalse '2 = ppPasteEnhancedMetAEile
Set myShape = pptSlide.Shapes(pptSlide.Shapes.Count)
If myShape.Height <> ExcRng.Height Then
myShape.Table.ScaleProportionally ExcRng.Height / 285
End If`
While I am doing it sets the font of the content gets bigger or smaller depending on the amount of text in the shape.
Can some one tell me if I can fix the size of the font to "8" irrespective of the amount of content.
Since its not a textarea but just a shape.
Shapes containing text in Powerpoint have the property Autosize:
ActiveWindow.Selection.ShapeRange().TextFrame.Autosize = ppAutoSizeNone
With this you can make the text stick to the size you determine in
ActiveWindow.Selection.ShapeRange().TextFrame.TextRange.Font.Size
I have a QTextEdit window with words and letters displayed in several colors. I want to be able to retrieve the color of each part of the text when processing the contents of the window. My attempt so far has been to save the entire contents as an html file and then parse through that to extract only the text with the color information. This is very cumbersome and difficult. I would much prefer to process the text using the QTextCursor if I could retrieve the color of the text at the cursor position. I have searched for the appropriate function but have not found one.
Is there a function to retrieve the color (or the format) at the QTextCursor position?
Or alternatively is there a way to retrieve each contiguous section of words and/or characters that have the same color (or format) with the format information?
Well I have found a way to do what I wanted. Here is the relevant code:
QTextCursor tc = qte->textCursor();
tc.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
while(tc.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor))
{
QTextCharFormat tcf = tc.charFormat();
int bg = tcf.background().color().rgb();
int fg = tcf.foreground().color().rgb();
printf("bg=%x fg=%x\n", bg, fg);
}
any comments or improvements are welcome.
[Corrected above]: I originally had
QColor bg = tcf.background().color().rgb();
QColor fg = tcf.foreground().color().rgb();
but with .rgb() on the end, it converts QColor to int.