No3371 / obsidian-regex-pipeline

An Obsidian plugin that allows users to setup custom regex rules to automatically format notes.
70 stars 8 forks source link

Feature Request: Extract Regexp Result #26

Closed zhao414 closed 1 year ago

zhao414 commented 1 year ago

Hi, thank you for bring us such a nice tool. obsidian-regex-pipeline is the only tool that can deal with the content within a note using regexp.

I use the note to store the logs. Sometime, I want to find the text with certain pattern within the log. However, rather than replace those matching text with some new text, I want to copy them into the clipboard.

Now, there is an idea: is it possible to extract each of the matching text(or strings) and append them one by one into the clipboard rather than replace them one by one with the given text in the template?

I think this will be very useful.

Looking forward to your reply.

No3371 commented 1 year ago

While it seems similar, it's actually a very different use case that can not be solved with effort less than rewriting or creating a whole new tool.

However, this can be done with editors like VSCode, here is an example:


`DP` Desolation Point
---------------------

| Console Name | Ingame Name | Hint |
| --- | --- | --- |
| `WhalingStationRegion` | Desolation Point |  |
| `CaveC` | Cave |  |
| `LighthouseA` | Lonely Lighthouse |  |
| `WhalingMine` | Abandoned Mine No.5 |  |
| `WhalingShipA` | The Riken |  |
| `WhalingWarehouseA` | Whale Processing |  |

`CH` Coastal Highway
--------------------

| Console Name | Ingame Name | Hint |
| --- | --- | --- |
| `CoastalRegion` | Coastal Highway |  |
| `QuonsetGasStation` | Quonset Garage |  |

So let's say I want to extract the Console Name of each entry...

  1. Paste the lines into VSCode
  2. Ctrl + F to open the Find panel
  3. Open the Replace panel by clicking on the left most folding arrow
  4. Turn on Regex mode by clicking on the button in the input field
  5. Search for ^\| `(.+)` .+\|$ and replace with >>>$1

Here is what the file would looks like then:

`DP` Desolation Point
---------------------

| Console Name | Ingame Name | Hint |
| --- | --- | --- |
>>>WhalingStationRegion
>>>CaveC
>>>LighthouseA
>>>WhalingMine
>>>WhalingShipA
>>>WhalingWarehouseA

`CH` Coastal Highway
--------------------

| Console Name | Ingame Name | Hint |
| --- | --- | --- |
>>>CoastalRegion
>>>QuonsetGasStation
  1. Search for ^(?!>>>).+$\n and replace with nothing
>>>WhalingStationRegion
>>>CaveC
>>>LighthouseA
>>>WhalingMine
>>>WhalingShipA
>>>WhalingWarehouseA

>>>CoastalRegion
>>>QuonsetGasStation
  1. Search for >>> and replace with nothing

WhalingStationRegion
CaveC
LighthouseA
WhalingMine
WhalingShipA
WhalingWarehouseA

CoastalRegion
QuonsetGasStation
  1. Search for ^$\n and replace with nothing
WhalingStationRegion
CaveC
LighthouseA
WhalingMine
WhalingShipA
WhalingWarehouseA
CoastalRegion
QuonsetGasStation
zhao414 commented 1 year ago

Does the plugin support : "Search for ^| (.+) .+|$ and replace with >>>$1"

the $1 is the thing that we want. So is there anyway to walkthrough the list of all the matches and output each of them directly?

Or can I use the plugin to perform all the steps above, for example, by putting all the steps in one script?

No3371 commented 1 year ago

Does the plugin support : "Search for ^| (.+) .+|$ and replace with >>>$1"

Yes it just call standard js regex.

Or can I use the plugin to perform all the steps above, for example, by putting all the steps in one script?

I suppose you can because they are all just regex replacements.

zhao414 commented 1 year ago

I made a update of the code to support this feature request. Please review.

for the example in your post, you can use:

(?<=^\|)(\s*`.+`\s*)(?=(\|.*\|.*\|$))

to match and extract the console name in one step.

image image