Skip to content

Latest commit

 

History

History
76 lines (48 loc) · 2.36 KB

File metadata and controls

76 lines (48 loc) · 2.36 KB

Using RegEx

Full RE2 syntax reference at > https://github.com/google/re2/wiki/Syntax
Go RegExp doc at > https://golang.org/pkg/regexp/
More about general concepts > https://www.regular-expressions.info/

Basic Regex

Basics of Regular Expressions

{% tabs %} {% tab title="Match" %} You can match a word by putting it between two brackets.

As example, this will only match the word "Dinosaur": (Dinosaur) {% endtab %}

{% tab title="Don't match" %} Using ?: after opening parenthesis of a capturing group creates a non-capturing group. Useful for example with template function reFindAllSubmatches.

This will not sub-match the words "red, blue, green":
{{ reFindAllSubmatches `(?:color=)(red|blue|green)` "color=red beautiful" }}

To clarify more - it will not show dateid, because it's a whole match:
{{ slice (index (reFindAllSubmatches `(?:dateid=)([0-9]{5})` "dateid=12345") 0) 1 }} {% endtab %}

{% tab title="Match A or B" %} You may also want to catch multiple options, for that we use a "Vertical bar" or also known as a "Pipe" between linux users.

As example, this will match if either "Cat" or "Dog" is present: (Cat|Dog) {% endtab %} {% endtabs %}

To match anything of any length, use .*.

Character classes

{% tabs %} {% tab title="Words" %} To match a word, you put it between two brackets.

Example: (Banana) {% endtab %}

{% tab title="Characters" %} For matching characters there are multiple options:

Matching specific characters:

For matching a specific character, you put them in square brackets.

This will match A, B and C: ([abc])

This will match every character from A-z: ([A-z])

This will match every number: ([0-9]) {% endtab %}

{% tab title="Special Characters" %} Sometimes you have to use special characters but it may cause conflicts. In this case, you will have to use an escape character.

For example, this is a star that doesn't interfere with other matches \*. {% endtab %} {% endtabs %}

Understanding Regex

If you still do not know what Regex are or want to know more. Check out the cheat sheet on the site below.

{% embed url="https://www.computerhope.com/jargon/r/regex.htm" %}

Great tools for writing and testing Regex

{% embed url="https://regex101.com/" %}