Finding File Names in a LaTex Document with a Regular Expression

Is there any way to filter Variable to escape RegEx special characters?

Only I would like to pick Up the name in my document.

I tried to use Expressions.app, RegExRX and regex.app., yesterday in this master.tex using in parallel with simplest command as bellow :

\documentclass{jarticle}
\begin{document}
 \include{content-1.tex} 
 \include{content-2.tex} 
 \include{content-3.tex} 
\end{document}

Using [ \{.+?} ] I found 5,

{jarticle} {document} {content-1.tex} {content-2.tex} {content-3.tex} .
{jarticle} {document} will be excluded from those choising.

Using {.+?}[^...] same result:

{jarticle} {document} {content-1.tex} {content-2.tex} {content-3.tex} .

I do not want to get neither {jarticle} nor {document}.
I would like to pick up just the "content-3.tex".

At the front of [\include{content-3.tex}], there is a kind of 1 spacing.
This is a standard procedure for LaTeX writing.
But if neccesary, I will take it off.

Can you tell me what kind of Regular Expression I should use?

And should I take off this space?
You will appreciate that.

thx and regards, WAKAMATSU (boehmflute)

Hey @boehmflute,

The best way to pose a regular expression question is to provide a real-world sample text and a sample text of the desired result.

The backslash is the escape character in regular expressions.

To make this task simpler I'd make multiple passes.

1) Find the data:

Find Pattern:

\\include\{.+?\.\w{3}\}

Result:

 \include{content-1.tex} 
 \include{content-2.tex} 
 \include{content-3.tex} 

2) Extract the meat:

Find Pattern:

.+\{([^}]+)\}

Replace Pattern:

\1

If I really wanted to do this in one pass I might do something like this:

(?<=\{)[^}]+?\.\w{3}(?=\})
(?<=\{)	==	Positive Look-Behind for “{”
[^}]+?	==	Any character NOT “}”, one or more NOT-greedy
\.		==	Literal dot
\w{3}	==	3 word characters
(?=\})	==	Positive Look-Ahead for “}”

Here's an example of a working macro:

Find SubStrings in Text -- Latex File Names v1.00.kmmacros (8.6 KB)

-Chris

Hey @boehmflute,

By the way – @JMichaelTX's macro in the previous thread shows how to escape special characters in a regular expression. He even cites @DanThomas as the originator:

Filter Variable to escape regular expression special characters

Of course reading regular expressions is like reading a foreign language when you first start trying to learn about them.

-Chris

1 Like

Just for fun...

Finding the requisite strings with the command line utility sed:

Find SubStrings in Text -- Sed -- Latex File Names v1.00.kmmacros (5.9 KB)

Finding the requisite strings with Perl:

Find SubStrings in Text -- Perl -- Latex File Names v1.00.kmmacros (5.9 KB)

-Chris

1 Like

Here's one that uses AppleScriptObjC and attempts to make the guts of the process less of a burden to the user.

The user need only supply the data source, the find pattern and the return pattern.

Although they might want to change the display in window result of the Execute an AppleScript Action to save to a variable or something else.

-Chris


Find SubStrings in Text Using a Regular Expression v1.00.kmmacros (9.5 KB)

Dear ccstone,
Thank you for your polite explanation.I am very encouraged.
How to use a RegEX has come a bit light for me.
I would like to use macros that you created, as soon as possible.
Since I was thinking,".tex" as an extension only , I was at a loss as to how to judge.I just learned "to search" by writing "three letters following".". Currently I am studying Jeffrey Friedl: "Mastering Regular Expressions". I am still reading the first 60 pages.
@JMichaelTX's macro, how to escape special characters. I have already read this article in this Forum.
But sometimes, only one[] or double[\] or more ?, I was unable to make a decision on the level of my knowledge about RegEX yet.
Could you tell me the following for the next step?
In order to rewrite "content - 1.tex" from the body of LaTeX and then "content - 4.tex" and the name "1 to 4", what kind of instructions should be applied as a RegEX statement Could it be with?

For the next goal, create a new file using the name of the changed file itself.
At the same time, add "content - 4. tex" after "content - 3. tex" in the main.tex body.
If possible, the modified .tex file name is written in {} of "\ include {}".

I'd like to know whether it is feasible or not.
Can I use "Keyboard Maestro Macros" to "write" in the body of "main.tex" file?
How do I pick a file from a specific folder?
How do I display the contents of the selected file?
In the displayed contents, "How do I write?"
I think these three steps are necessary, is this a reasonable direction?
I am waiting for your advice.
Yours, WAKAMATSU (boehmflute)
P.S
I will study more.

Hey @boehmflute,

I really don't recommend that book to beginners. Friedl is very learnèd, but his book and writing style is not really for neophytes (in my opinion).

Check out the references on our wiki page for regular expressions.

I personally prefer BBEdit as a starting place for working with regular expressions. (The commercial demo reverts to BBEdit-Lite after 30 days, but it is still very powerful and AppleScriptable.)

This is very unclear to me.

Please provide before and after examples of your text.

Similarly to this:

I want to change this text in my LaTeX document:

\include{content-1.tex}

Into this:

\include{content-4.tex}

If I understand correct then yes.

Yes.

There are several ways.

It depends upon what you want to do with it.

Keyboard Maestro can display text in a floating window.

Personally I prefer to use BBEdit if I'm going to work with the text, because BBEdit is very powerful and also AppleScriptable – and this allows me to do many things most Mac users couldn't imagine.

You're asking too many questions at once.  :wink:

-Chris

You can easily do this with either a native KM Action, or script:

image

  tell application "Finder"
    set filePosixPath to POSIX path of (choose file with prompt #~withPromptValue~# ¬
  	of type #~ofTypeValueList~# ¬
  	default location #~defaultLocationValue~# ¬
  	invisibles #~invisiblesBoolean~# ¬
  	multiple selections allowed #~multipleSelectionsAllowedBoolean~# ¬
  	showing package contents #~showingPackageContentsBoolean~#)

  end tell

the text between the #~SomePrompt~# are placeholders for the value you want to use.

From the Standard Additions Scripting Dictionary in Script Editor:


If it is a text file, the simplest way is:

image


BTW, all of this stuff is fairly easy to discover, either from the KM Editor app, or the Keyboard Maestro Wiki.

In the KM Editor, when you are in a "Add Action" field, or have an Action selected, pressing ⌘⌃A will display a search prompt for actions. So if you type in "file", for example, you will get all Actions that relate to files:

Questions?

Dear JMichaelTX,
Thank you for your gracious answer.
I will read your explanation carefully and once again.
Thank you for always having meaningful advices.
Best regards, WAKAMATSU (boehmflute)

4 posts were split to a new topic: How to Learn Regular Expressions (RegEx)