Skip to content

Commit

Permalink
Add gsub regex example
Browse files Browse the repository at this point in the history
  • Loading branch information
nntrn committed Apr 18, 2024
1 parent f5f1682 commit cd87935
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions _functions/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,26 @@ def dquote($text): "\"\($text)\"";
def unsmart($text): $text | gsub("[“”]";"\"") | gsub("[’‘]";"'");
def unsmart: . | unsmart;
```

## Cleaning up text


Remove repeated special characters using `\\1` (such as `[[`, `{{`, `'''`)

```jq
gsub("([^\\w\\d\\s])\\1(\\1)?";"")
```
```console
$ cat wikitext.txt
{{Date table sorting|December 31}}
[[Charlotte, North Carolina|Charlotte]] (4)
[[North Carolina]]
'''5'''

$ jq -rR 'gsub("([^\\w\\d\\s])\\1(\\1)?";"")' gsub.txt
Date table sorting|December 31
Charlotte, North Carolina|Charlotte (4)
North Carolina
5
```

0 comments on commit cd87935

Please sign in to comment.