Other Tools

Other text tools!

In addition to the text manipulation tools that we've uncovered, we have several other tools that you can use easily in your programs.

Regular expression tool

All regular expression tools can be found in the RegexTools class that you can find within the Textify.Tools namespace. However, they are not extensions to strings.

IsValidRegex()

public static bool IsValidRegex(string pattern)
public static bool IsValidRegex(Regex pattern)

This function lets you determine whether the specified regular expression pattern contains a valid syntax. This is useful for easy verification of the regex pattern syntax to ensure that you can control the situation where the user code tried to provide invalid regular expression pattern. The Regex class by default doesn't validate the syntax until you attempt to match a string with it, so we've made a wrapper to simplify things by testing a specified pattern against an empty string.

  • ^[\w.-]+$ returns true

  • ^[\w.-"]+$ returns false

  • Empty regexes also return true

ParseRegex() and TryParseRegex()

public static Regex ParseRegex(string pattern)
public static bool TryParseRegex(string pattern, out Regex? result)

These two functions allow you to parse a regular expression, returning an instance of a Regex class once it's been validated using the above function, IsValidRegex().

  • ParseRegex() throws an exception when an invalid regular expression syntax is detected.

  • TryParseRegex() returns a boolean indicating that either the operation succeeded (true) or failed (false). When the operation fails, the resulting value is null.

IsMatch(), Match(), and Matches()

These three functions allow you to check to see if the regular expression engine finds matches in the provided regular expression pattern tested against the text.

  • IsMatch() returns true if there is a match; otherwise, false.

  • Match() returns the first match is there is a match.

  • Matches() returns all matches that are found.

Filter()

The above functions allow you to replace all matches in the text with either the replacement string or the empty string.

  • The first function overload replaces all matches with an empty string

  • The second function overload replaces all matches with the provided replacement string

Split()

The above function allows you to split the text using the regular expression pattern matches as the delimiter.

String syntax attribute

First introduced in .NET 7, you can use the string syntax attribute, called StringSyntaxAttribute, to give your strings in the function context, but it's only available in the source code level. This depends on your IDE and how it supports this attribute.

Here's a simple example of how to use it:

You can use this syntax attribute in not only .NET 7.0+ apps, but you can also use it in .NET Standard and .NET Framework applications.

If everything works properly, you should be able to see that parts of your string, such as regex in the below screenshot, are highlighted in different colors, and warnings should work, too.

JSON tools

All JSON tools can be found in the JsonTools class that you can find within the Textify.Tools namespace. However, they are not extensions to strings. They are wrapped against Newtonsoft.Json.

BeautifyJson() and MinifyJson()

Using these two functions, you can perform the following operations on a JSON file:

  • BeautifyJson() returns a string that consists of a beautified version of your JSON file that you've passed as an argument.

  • MinifyJson() returns a string that consists of a minified version of your JSON file that you've passed as an argument.

BeautifyJsonText() and MinifyJsonText()

Using these two functions, you can perform the following operations on a JSON representation:

  • BeautifyJsonText() returns a string that consists of a beautified version of your JSON text that you've passed as an argument.

  • MinifyJsonText() returns a string that consists of a minified version of your JSON text that you've passed as an argument.

FindDifferences()

This function returns a JSON object that consists of collected differences between two different JSON tokens. If the JSON objects are the same, it returns an empty JSON object. Otherwise, it returns either a list of additions, deletions, or modifications if the target object type is an object, an array of additions and deletions if the target object type is an array, or an array of the whole object difference if the target object type is something else. To clarify this, here's an example of a difference between two objects:

A difference JSON object in this state can be described like this:

  • +<PropertyName>: Indicates an addition

    • A JSON object with a single property, +, with the value that holds the name of the property.

  • -<PropertyName>: Indicates a removal

    • A JSON object with a single property, -, with the value that holds the name of the property.

  • *<PropertyName>: Indicates a modification

    • A JSON object with a single property, *, that has a JSON object that has the following two properties:

      • source: Old value of the property

      • target: New value of the property

Another example of a difference between two arrays:

A difference JSON object in this state is an object that contains two properties:

  • +: Indicates additions

  • -: Indicates removals

Another example of a difference between two values "test" and "test2":

A difference JSON object in this state is an object that contains two properties:

  • +: Indicates a value of the target

  • -: Indicates a value of the source

Last updated