Regex replace icm externe perl-aanroep

Pagina: 1
Acties:

Onderwerpen


Verwijderd

Topicstarter
In de osx-editor textmate kan je custom commands definieren. Ik gebruik deze editor vooral voor latex en wil een soort automatische manier vinden om in m'n latex bestand het totaal aantal woorden in te voegen, elke keer dat ik de master-file compile.

Ik heb nu een custom command aangemaakt die ik elke keer handmatig moet aanroepen met een sneltoets voor het compilen:

dan word de volgende code uitgevoerd:

code:
1
perl -T /usr/local/bin/texcount.pl -1 -sum -inc "$TM_FILEPATH"


eigenlijk zou ik willen dat voordat ik compile er in het bestand gezocht word naar

\def \wordcount {xxx} (waar xxx een nummer of helemaal leeg is) en dat xxx dan vervangen wordt door hetgeen wat bovenstaande code teruggeeft (een nummer)

het compile command bevat de volgende code:

code:
1
2
3
4
5
6
7
8
9
10
11
12
# Source some support functions we need.
. "${TM_SUPPORT_PATH}/lib/html.sh"
. "${TM_SUPPORT_PATH}/lib/webpreview.sh"

# Prepare output window.
html_header 'Typeset & View' "$FILE"

# Compile.
texMate.py latex 1
RC=$?
html_footer
exit $RC


dus daarin zou de vervanging plaats moeten vinden. Code om een bestand in te lezen enzo is niet nodig omdat dat door de commando's al afgehandeld wordt, het gaat dus alleen om de code om xxx met de output van de externe file-aanroep te vervangen.

Is dit uberhaupt mogelijk? Ik ben wel enigszins bekend met regular expressions, maar voornamelijk binnen php.

  • CMG
  • Registratie: Februari 2002
  • Laatst online: 10-12-2024

CMG

Qua regular expressions kan ik je wel de goeie weg op sturen:
Performing a regex search-and-replace is just as easy:

$string =~ s/regex/replacement/g;
I added a "g" after the last forward slash. The "g" stands for "global", which tells Perl to replace all matches, and not just the first one. Options are typically indicated including the slash, like "/g", even though you do not add an extra slash, and even though you could use any non-word character instead of slashes. If your regex contains slashes, use another character, like s!regex!replacement!g.

You can add an "i" to make the regex match case insensitive. You can add an "s" to make the dot match newlines. You can add an "m" to make the dollar and caret match at newlines embedded in the string, as well as at the start and end of the string.

Together you would get something like m/regex/sim;
Bron: http://www.regular-expressions.info/perl.html

Die site is erg fijn en het is het zeker waard om helemaal te lezen.

NKCSS - Projects - YouTube