Skip to content

fabianokoseki/regex-quick-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 

Repository files navigation

Some good links

To learn regex
https://regexone.com/

To parse regex
https://regex101.com/

Good straight to the point tutorial(in portuguese)
https://aurelio.net/regex/apostila-conhecendo-regex.pdf

Quick reference

\d = any digit
\w = any word character([a-zA-Z0-9_])
\S = any non-whitespace character
\b = Matches, without consuming any characters, immediately between a character matched by \w and a character not matched by \w (in either order). It cannot be used to separate non words from words.
. = matches any character (except for line terminators)
^ = asserts position at start of a line
$ = asserts position at end of a line
() = capturing group
? = optional, matches between zero and one times.
* = can exist in any quantity
+ = at least once
[] = list of characters
[^] = negated list: anything inside that list should tell that it shouldn't appear
[-] = interval list: character between the range given
{} = quantifier, matches any times by the input inside the braces

Examples

1. Insert bolding from markdown(**word**) between the words before the "=" sign

\d = any digit  
\w = any word character([a-zA-Z0-9_])  
. = matches any character (except for line terminators)
^ = asserts position at start of a line
$ = asserts position at end of a line
() = capturing group
? = optional, matches between zero and one times.
* = can exist in any quantity
+ = at least once
[] = list of characters
[^] = negated list: anything inside that list should tell that it shouldn't appear
[-] = interval list: character between the range given
{} = quantifier, matches any times by the input inside the braces

Desired output:

**\d** = any digit
**\w** = any word character([a-zA-Z0-9_])
**.** = matches any character (except for line terminators)
**^** = asserts position at start of a line
**$** = asserts position at end of a line
**()** = capturing group
**?** = optional, matches between zero and one times.
***** = can exist in any quantity
**+** = at least once
**[]** = list of characters
**[^]** = negated list: anything inside that list should tell that it shouldn't appear
**[-]** = interval list: character between the range given
**{}** = quantifier, matches any times by the input inside the braces
Click to see the answer(gnu-sed)

$ sed -E 's/(^\S*)/**\1**/'

(^\S*): Using capturing group "()" matches any non-whitespace character, from zero to unlimited times;

**\1**: Get the captured group \1, and put "**" between it.

improve: check if it is possible to put a "\" if the captured group has a "*"(to not conflict the character * with the reserved * from markdown)

About

Quick guide to learn how to use the basics of regex

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published