A Journal Through My Activities, Thoughts, and Notes
#csharp #caller #tips

public void DoProcessing()
{
TraceMessage("Something happened.");
}

public void TraceMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Trace.WriteLine("message: " + message);
Trace.WriteLine("member name: " + memberName);
Trace.WriteLine("source file path: " + sourceFilePath);
Trace.WriteLine("source line number: " + sourceLineNumber);
}

// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Visual Studio Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31


That's quite handy, isn't it? Reference
#grep #tips

## Basic Regular Expressions (BRE)

Basic Regular Expressions are the default regular expression syntax used by grep when no special options are provided. BRE uses a more limited set of metacharacters compared to Extended Regular Expressions (ERE).

Some key points about BRE:

- Metacharacters like +, ?, |, (, ) are treated as literal characters and need to be escaped with a backslash (\) to use their special meaning.

- The $$ and $$ constructs are used for grouping in BRE, instead of just ( and ) as in ERE.

- Alternation is done using \| instead of just |.

- Repetition operators like * and \{n,m\} are used to match zero or more and ranges of repetitions respectively.

For example, to match lines containing either "cat" or "dog" using BRE:

grep 'cat\|dog' filename


And to match lines with 3 to 6 digits:

grep '\([0-9]\{3,6\}\)' filename


The backslashes are necessary to use the grouping and repetition metacharacters in BRE syntax.

So in summary, BRE provides a more limited set of regex features compared to ERE, but is still very powerful for common text matching tasks. The main difference is the need to escape certain metacharacters in BRE.

One thing I would to mention here is that most metacharacters in Basic Regular Expressions (BRE) need to be escaped, but the * character does not. This might seem inconsistent, but it's due to historical reasons and the way BRE was designed.

### Historical Context

In the early days of Unix, the grep command was developed, and it used BRE as its default syntax. At that time, the * character was already a special character in many programming languages and shell scripts, often used for wildcard matching. To avoid conflicts and make BRE more intuitive for users familiar with shell globbing, the designers of grep decided to treat the * character as a special character without needing an escape.

### Why Not Escape *?

Escaping every special character would make BRE more verbose and less readable, especially for simple patterns. By treating * as a special character without an escape, it simplifies common tasks like matching any number of characters (including zero) before or after another character.

### Example

For instance, to match any number of characters before or after a specific string, you can use:

grep 'pattern*' filename


This will match any line containing "pattern" followed by zero or more characters.

### Summary

While most metacharacters in BRE require escaping, the * character is an exception due to its historical significance and the desire to keep BRE syntax simple and intuitive. This makes it easier for users to learn and use basic regular expressions without needing to worry about escaping every special character.
#grep #tips
The -o option in grep stands for "only matching." When this option is used, grep will print only the parts of the lines that match the specified pattern, rather than the entire line. This is particularly useful when you want to extract specific substrings from lines of text.

The -E option in grep enables the use of Extended Regular Expressions (ERE) in the pattern. This allows for more powerful and flexible pattern matching compared to basic regular expressions (BRE), which is the default behavior of grep.

### Key Differences with Extended Regular Expressions

When using -E, you can utilize additional metacharacters and constructs that are not available in basic regular expressions. Here are some of the key features of ERE:

1. Metacharacters: In ERE, the following metacharacters are treated as special without needing to be escaped:
- + : Matches one or more occurrences of the preceding element.
- ? : Matches zero or one occurrence of the preceding element.
- | : Acts as a logical OR between expressions.
- () : Groups patterns for applying quantifiers or for alternation.

2. Example Usage:
- To match either "cat" or "dog", you can use:
     grep -E "cat|dog" filename


- To match one or more digits, you can use:
     grep -E "[0-9]+"


3. Combining with Other Options: You can combine -E with other options like -i for case insensitivity or -o to print only the matching parts:
   grep -Eio "cat|dog" filename
The following c# code splits the clientIds into smaller lists, each containing up to 100 elements.

var listOfClientIdLists = clientIds
.Select((v, i) => new { Value = v, Index = i })
.GroupBy(g => g.Index / 100, gi => gi.Value)
.Select(chunk => chunk.ToList())
.ToList();


### Explanation

1. Select((v, i) => new { Value = v, Index = i }):

- This uses the Select method with an overload that provides both the element (v) and its index (i).
- It transforms the clientIds collection into a new collection of anonymous objects, each containing:
- Value: the original element (v).
- Index: the position of the element in the original list (i).

2. GroupBy(g => g.Index / 100, gi => gi.Value):

- The GroupBy method organizes the elements into groups.
- g => g.Index / 100: This lambda expression determines the group key by dividing the index by 100. This effectively groups the elements into chunks of 100.
- gi => gi.Value: This specifies that only the Value part of the anonymous object should be included in the groups.

3. Select(chunk => chunk.ToList()):

- This converts each group (or chunk) into a list.

4. ToList():

- This final ToList converts the collection of lists into a list of lists.

### Alternative
Claude.AI suggests a better way to achieve the same goal, which has better simplicity and readability.
var listOfClientIdLists = clientIds
.Chunk(100)
.Select(chunk => chunk.ToList())
.ToList();

#csharp #tips
#regex #tips
JavaScript does not have a single-line modifier. Use \S\s instead of a dot if you want to match any character including newlines.
#flutter #initState #tips
The error you're encountering is caused by trying to update the UI (noteModel.content = '#${noteModel.initialTag}\n';) within the initState method, which triggers a rebuild of the widget while it is still in the build phase. This is not allowed in Flutter, as it can lead to inconsistent states.

To resolve this issue, you should delay the update until after the build phase. You can achieve this by using WidgetsBinding.instance.addPostFrameCallback, which schedules the code to run after the current frame has been rendered.
 
 
Back to Top