|
Ok. But a brief reading of the link I posted suggests that will changing.
|
|
|
|
|
Personally, I'd probably still avoid it unless it solved a very significant problem for me because I like being able to make 17KB dlls that are actually useful.
Metadata ain't free.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
honey the codewitch wrote: I'd probably still avoid it unless it solved a very significant problem
Specific times I have used it in other languages.
- Dynamic loading of classes. Especially when parameter lists are dynamic.
- Messing with private data of a class. Most the time for unit tests, but I think one time it was in the production code. In C++ there is a way to do that anyways but it always risky and just as hacky.
honey the codewitch wrote: Metadata ain't free.
Which is why it has taken so long I suspect.
|
|
|
|
|
I am asking a question about regular expression.
I am NOT looking for references to AI regular expression generators.
Here in my text to find matches of ALL "hci" in it:
"Devices:\n\thci1\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"
Here is my attempt to accomplish that task
QRegularExpression re("(hci[0-9]+)");
It matches only first occurrence of hci1" and twice
Please follow the established CodeProject instruction and read the post.
PLEASE no references to AI regular expression generators.
I need to learn TO BUILD regular repression - specially how to write it so it will attempt to match multiple items.
PS I did try to use "global match" , no go.
|
|
|
|
|
Salvatore Terress wrote: Please follow the established CodeProject instruction and read the post.
PLEASE no references to AI regular expression generators.
Please stop giving us orders. You have already been kicked off this forum once for your bad attitude. Everyone who tries to answer questions here does it in their own time and at no cost to you. If the answer is not what you were hoping for then feel free to ignore it.
|
|
|
|
|
Why didn't you post the code?
I mean, you commented
But only the regular expression constructor is shown.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Salvatore Terress wrote: It matches only first occurrence of hci1" and twice
Did you mean something like 'it only matches once but there are two'?
Salvatore Terress wrote: I need to learn TO BUILD regular repression
Your regular expression is correct.
So that means your usage of the regular expression engine/library is incorrect. Nothing to do with the regular expression itself
Typically an engine/library will have an iteration idiom where each loop matches the next one.
I didn't look at all but the following google seems to return results that would be relevant.
{code}
"QRegularExpression" iterate through matches
{code}
|
|
|
|
|
Since my last post is nowhere to be found here...
I do appreciate all help resolving the issue.
And since posting code was requested, here it is.
It generally works retrieving multiple matches...
such as "hci1 hci0 "
what is missing is to retrieve PAIR of matches
such as
"hci1 00:xx:yy...
in other words I can retrieve name or address but not BOTH.
I would be grateful if somebody can give me the actual code and explanation how to match PAIR of values.
As can be seen - I did try different placement of parentheses
" (...)" but it did not work.
{
text = " START MATCH NAME AND ADDRESS CODE BLOCK ";
textEditPtr_DEBUG->append(text);
#ifdef TASK
DELETED
#endif
QString word;
QStringList words;
QString pattern = "Devices:\n\thci1\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n";
QRegularExpression re("(([0-F]{2}[:]){5}[0-F]{2})");
QRegularExpressionMatchIterator i = re.globalMatch(pattern);
text = " HAS MATCH MULTI";
qDebug()<< text;
textEditPtr_DEBUG->append(text);
#ifdef BYPASS no go
foreach(auto item,i)
{
text = " TEST foreach ";
text += item;
qDebug()<< text;
textEditPtr_DEBUG->append(text);
}
#endif
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
word = match.captured(1);
words << word;
qDebug()<< word;
text = " Full match name and address... ";
text += word;
qDebug()<< text;
textEditPtr_DEBUG->append(text);
}
#ifdef BYPASS
else
{
text = " NO MATCH MULTI";
}
#endif
qDebug()<< text;
text = " END MATCH ADDRESS CODE BLOCK ";
textEditPtr_DEBUG->append(text);
}
|
|
|
|
|
Salvatore Terress wrote: Here in my text to find matches of ALL "hci" in it:
So based on your sub - reply to me
"Devices:\n\thci1\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"
What you actually want is the following
hci 00:50:B6:80:4D:5D
hci 00:15:83:15:A2:CB
Your text sample represents a multi-line input presumably with tab separators.
From that sample it is actually pointless to use a regex since normal parsing (or csv parser would work.)
But in terms of Regex you have several problems
1. Dealing with multiple lines
2. Correctly matching the values.
3. Dealing with special characters (including perhaps #1 above.)
4. Dealing with a list of results - as I stated in my other reply.
So your regex is not even close to correct for matching the address.
Looking at 2 only because that is the part most likely to be usable as a regex the equivalent regex for Perl would look like the following. Since you are not using Perl yours might vary but I suspect not.
(hci\w+([a-fA-F0-9][a-fA-F0-9]:)+[a-fA-F0-9][a-fA-F0-9])
There are variations on the above
- Making the tab explicit
- Restricting the id to the exact length
- variation on the hex digit but I prefer the above form
|
|
|
|
|
Thanks, but it still does not do what I want, as you expected it may. .
Would it be OK to actually discuss and analyze this ?
I am asking I am NOT telling the group shoud do that.
QRegularExpression re("(hci\w+([a-fA-F0-9][a-fA-F0-9] +[a-fA-F0-9][a-fA-F0-9])");
There are , imho , important parts (to reg exp ) and they are ok when used alone.
1. If I opt to use QRegularExpressionMatchIterator i = re.globalMatch(pattern);
There should be at lest three "code groups " delimited by "(,,,code group... )"
2. there are TWO base "reg expressions " one working OK as is
"hci[0-9]" - function as "match hcix where x = [0-9] - some doc call [0-9] range
or
hci\w should perform SAME task
3. the analysis of "xx:" using [a-fA-F0-9] is simply "too cute " and [0-F] with "how many times do the previous - {2} works as well.
I have no need to check for lower case "hexadecimal " since that actually does not exist anyway...
So - I will , perhaps stubbornly, try to continue usage of at least ONE of the QT classes and maybe will
find correct modification of the above.
If i end up with plain "reg_exp" so be it...
Thanks
|
|
|
|
|
When you post code use code blocks.
Salvatore Terress wrote: If I opt to use QRegularExpressionMatchIterator i = re.globalMatch(pattern);
Not sure how to emphasize what I have already said several times.
That question is NOT about regular expressions.
It is about how to use that specific library.
I suggest you start with a less difficult example to figure out how iteration works. And google for examples as I documented in the other reply.
Salvatore Terress wrote: There should be at lest three "code groups " delimited by "(,,,code group... )"
You mean the parens. Parens are used for 'capturing' and for 'grouping'.
For the case I gave the inner ones was to group the outer for capturing.
And yes that is a problem when you iterate. The library you are using might have a way to specify that the parens are not 'capturing'.
In Perl it looks like the following. There would be only one capturing group which would match either 'abcxyz' or 'defxyz'.
((?:abc|def)xyz)
I will note that I use that feature so rarely that I had to look it up.
Salvatore Terress wrote: the analysis of "xx:"
As I mentioned there are variations to what I suggested. Myself I don't care for using '{2}' in a case like this. Just a preference.
(Had to edit the above because I messed up the code block)
modified 16-Nov-23 10:40am.
|
|
|
|
|
SOLVED and CLOSED
as initially asked for , by using named subgroup option.
QRegularExpression re("(((?<one>hci[0-9])(.*))?(?<two>(([0-F]{2}[:]){5}[0-F]{2}))(.*))");
Thanks very much for all support given.
|
|
|
|
|
int AnInt=6;
void EditInt(int * IntPnt)
{
*IntPnt=7;
}
EditInt(&AnInt);
Is this how you should proceed?
|
|
|
|
|
That's passing as a pointer, not as a reference. As a reference (C++ only, though maybe C now has them too), it's
void EditInt(int& IntRef)
{
IntRef=7;
}
int AnInt=6;
EditInt(AnInt);
|
|
|
|
|
Greg Utas wrote: That's passing as a pointer, not as a reference. Which is just the same, apart from semantics.
|
|
|
|
|
Very true. A reference can even be to nullptr , and you need to check for this if writing bulletproof code:
void function(type& arg)
{
if(&arg == nullptr)
throw std::invalid_argument("function received null argument");
}
type* nasty = nullptr;
function(*nasty); Without the check, function crashes when it gets around to using arg .
|
|
|
|
|
And the same with a pointer, since there is no material difference between the two.
|
|
|
|
|
I would assert() something like that rather than throw .
It strikes me as an error in the code itself, rather than something that should have filtered its way into runtime code, so I think assert is more appropriate.
As I understand it, best practices indicate that a reference should never be deliberately null. Could be wrong here?
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
What to do depends on the environment you're running in. I've never used assert , so I had to look up the documentation, which says that it calls abort . Most application code won't catch an invalid_argument exception, so the effect will be similar.
In my open-source software, I neither throw nor assert in this situation. Instead, I generate a debug log with a traceback and return whatever signifies failure if the function has a result. That's appropriate if avoiding crashes is paramount, which is what my software aims for.
You're right that a reference shouldn't be null. For many functions, a pointer argument shouldn't be null either. But applications misbehave, so you have to decide whether your function will check for bad arguments or just crash.
|
|
|
|
|
|
I'm looking at old code.
I don't think it's something I ever done.
It seems weird that the compiler accept to not fully initialize the struct.
They (old retired programmers) seem to expect the values to be zero-initialized.
I find it not really safe.
I caught that with the clang/tidy checker.
<h1>define sz_Potato "Patate"</h1>
struct MyStruct
{
int a;
int b;
const char* text;
short c;
};
MyStruct myStructArray[3] =
{
{1, 2},
{1, 2, sz_Potato},
{1, 2, sz_Potato, 4}
};
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
If your code should be compiled as C++ code (not plain C), do yourself a favour and paste a line like:
struct MyStruct
{
MyStruct () {memset (this, 0, sizeof(MyStruct));} int a;
int b;
const char* text;
short c;
}; Otherwise C initialization rules are vague enough to let you wonder what is going to happen. For instance, in your example, if myStructArray is static, it will get initialized with zeroes. If it's stack allocated, it will be uninitialized.
And to answer your question, yes your code is valid C and C++ code. Not nice but valid.
Mircea
|
|
|
|
|
yeah, I know and will add default contstructors.
Thanks.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Maximilien wrote: It seems weird that the compiler accept to not fully initialize the struct.
They (old retired programmers) seem to expect the values to be zero-initialized.
According to cppreference.com:[^] Quote: All members that are not initialized explicitly are empty-initialized.
The old programmers are correct.
|
|
|
|
|
Thanks.
let's hope 0 or empty strings are not valid values!!
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|