ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* String substitution using regular expressions and backreferences
@ 2022-08-01 19:58 Thangalin via ntg-context
  2022-08-01 20:13 ` Wolfgang Schuster via ntg-context
  0 siblings, 1 reply; 5+ messages in thread
From: Thangalin via ntg-context @ 2022-08-01 19:58 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Thangalin


[-- Attachment #1.1: Type: text/plain, Size: 1429 bytes --]

Hi list,

I'm looking to perform text replacements.

\definereplacement[SubstPostmeridian][
  match={[Pp].[Mm].]},
  replace={\cap{pm}}
]

The \replaceword command doesn't handle periods well. The translate module
doesn't seem flexible enough to cover edge cases. Consider the following
example document containing both sample inputs and sample outputs:

\starttext
  {\bf Markdown Input}

  Our grandmother clock rang 11 p.m. and we fled.

  Our grandmother clock rang 11 p.m., so we fled.

  Our grandmother clock rang 11 p.m. We fled.

  \blank[big]

  {\bf \ConTeXt{} Output}

  Our grandmother clock rang 11 \cap{pm} and we fled.

  Our grandmother clock rang 11 \cap{pm}, so we fled.

  Our grandmother clock rang 11 \cap{pm}. We fled.
\stoptext

It would be most convenient to write:

% Strip periods from p.m.
\definereplacement[SubstPostmeridianLowercase][
  match={[Pp].[Mm]. ([^:upper:])},
  replace={\cap{pm} \1}
]

% Preserve terminal period for p.m. (e.e. cummings notwithstanding)
\definereplacement[SubstPostmeridianTerminal][
  match={[Pp].[Mm]. ([:upper:])},
  replace={\cap{pm}. \1}
]

% Apply a macron for lowercase 'c' (McAnulty, McGenius, etc.)
% Well, not quite a macron: https://tex.stackexchange.com/q/364024/2148
\definereplacement[SubstMac][
  match={Mc([:upper:]\w)},
  replace={M\macronbelow{c}\1}
]

The \1 may be problematic. Other sigils include $1 and #1, which may also
have issues.

Thank you!

[-- Attachment #1.2: Type: text/html, Size: 3056 bytes --]

[-- Attachment #2: Type: text/plain, Size: 496 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-08-26  7:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-01 19:58 String substitution using regular expressions and backreferences Thangalin via ntg-context
2022-08-01 20:13 ` Wolfgang Schuster via ntg-context
2022-08-01 20:56   ` Thangalin via ntg-context
2022-08-25 19:44   ` Thangalin via ntg-context
2022-08-26  7:34     ` Hans Hagen via ntg-context

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).