获取花括号内的字符串

获取花括号内的字符串

我有一个函数,它接受其他命令的参数。然后我将参数拆分为参数中的第二个命令。现在我需要获取赋予此命令的参数。所以我有一个以花括号开头的字符串,并且在字符串的某个地方它是闭合的。所以我想获取这些花括号之间的这个字符串并将其保存在变量中。例如

\edef\foo{{this is the string I want} This not}

编辑:添加了示例

答案1

\regex_extract_once:nVN可以使用和正则表达式提取文本\{(.+)\}。然后由序列中的第二项给出所需的文本\l_tmpa_seq

在此处输入图片描述

\documentclass[border=6pt]{standalone}
\begin{document}
\edef\foo{{this is the string I want} This not}
\ExplSyntaxOn
\cs_generate_variant:Nn \regex_extract_once:nnN { nVN }
\regex_extract_once:nVN { \{(.+)\} } \foo \l_tmpa_seq
\seq_item:Nn \l_tmpa_seq { 2 }
\ExplSyntaxOff
\end{document}

编辑:下面\extract定义了一个命令来处理用户输入。

\documentclass[border=6pt]{standalone}
\ExplSyntaxOn
\NewDocumentCommand \extract { m }
  {
    \regex_extract_once:nnN { \{(.+)\} } {#1} \l_tmpa_seq
    \seq_item:Nn \l_tmpa_seq { 2 }
  }
\ExplSyntaxOff
\begin{document}
\extract{{this is the string I want} This not}
\end{document}

答案2

据我所知,你想要一些比迄今为止建议的答案更简单的东西。


\def\defunwanted#1\foo{\def\unwanted{#1}}
\def\foo#1{%
  \afterassignment\defunwanted
  \def\wanted#1\foo}
  
  \foo{{this is the string I want} This not}

  \show\wanted

  \show\unwanted

  \bye

显示\wantedthis is the string I want 并且 \unwantedThis not

答案3

您可以使用 来做到这一点expl3。如果作为参数给出的宏的替换文本不以{(显式括号) 开头,则仅??返回 (它可以变成错误消息)。

如果有一个可选参数,一个宏名,则它被定义为包含括号组中的标记。

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\extractfirstgroup}{om}
 {
  \jonibue_extractfirst:o { #2 }
  \IfValueTF { #1 }
   { \tl_new:N #1 \tl_set_eq:NN #1 \l__jonibue_extractfirst_tl }
   { \tl_use:N \l__jonibue_extractfirst_tl }
 }

\tl_new:N \l__jonibue_extractfirst_tl

\cs_new_protected:Nn \jonibue_extractfirst:n
 {
  \tl_clear:N \l__jonibue_extractfirst_tl
  \tl_if_head_is_group:nTF { #1 }
   {
    \__jonibue_extractfirst:w #1 \q_stop
   }
   {
    ??
   }
 }
\cs_generate_variant:Nn \jonibue_extractfirst:n { o }

\cs_new_protected:Npn \__jonibue_extractfirst:w #1 #2 \q_stop
 {
  \tl_set:Nn \l__jonibue_extractfirst_tl { #1 }
 }

\ExplSyntaxOff

\begin{document}

\newcommand\foo{{this is the string I want} This not}
\newcommand\baz{No group}

\extractfirstgroup{\foo}

\extractfirstgroup[\xyz]{\foo}
\texttt{\meaning\xyz}

\extractfirstgroup{\baz}

\end{document}

在此处输入图片描述

答案4

如果我必须在没有 LuaTeX 或 eTeX 扩展且没有 expl3 编程层的传统引擎上执行此操作,也许我会这样做:

\makeatletter
%%=============================================================================
%% PARAPHERNALIA:
%% \UD@firstoftwo, \UD@secondoftwo, \UD@PassFirstToSecond, \UD@Exchange,
%% \UD@removespace, \UD@stopromannumeral, \UD@CheckWhetherNull,
%% \UD@CheckWhetherBrace, \UD@CheckWhetherLeadingExplicitSpace,
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
\newcommand\UD@Exchange[2]{#2#1}%
\@ifdefinable\UD@removespace{\UD@Exchange{ }{\def\UD@removespace}{}}%
\@ifdefinable\UD@stopromannumeral{\chardef\UD@stopromannumeral=`\^^00 }%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\UD@stopromannumeral\UD@secondoftwo}{%
  \expandafter\UD@stopromannumeral\UD@firstoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether argument's first token is an explicit character token 
%% of category 1 (begin group):
%%.............................................................................
%% \UD@CheckWhetherBrace{<Argument which is to be checked>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked has a leading
%%                        explicit catcode-1-character-token>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked does not have a
%%                        leading explicit catcode-1-character-token>}%
\newcommand\UD@CheckWhetherBrace[1]{%
  \romannumeral\expandafter\UD@secondoftwo\expandafter{\expandafter{%
  \string#1.}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\UD@stopromannumeral\UD@firstoftwo}{%
  \expandafter\UD@stopromannumeral\UD@secondoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether brace-balanced argument's first token is an explicit
%% space-token:
%%.............................................................................
%% \UD@CheckWhetherLeadingExplicitSpace{<Argument which is to be checked>}%
%%                                     {<Tokens to be delivered in case <argument
%%                                       which is to be checked> does have a
%%                                       leading explicit space-token>}%
%%                                     {<Tokens to be delivered in case <argument
%%                                       which is to be checked> does not have a
%%                                       a leading explicit space-token>}%
\newcommand\UD@CheckWhetherLeadingExplicitSpace[1]{%
  \romannumeral\UD@CheckWhetherNull{#1}%
  {\expandafter\UD@stopromannumeral\UD@secondoftwo}%
  {%
    % Let's nest things into \UD@firstoftwo{...}{} to make sure they are nested in braces
    % and thus do not disturb when the test is carried out within \halign/\valign:
    \expandafter\UD@firstoftwo\expandafter{%
      \expandafter\expandafter\expandafter\UD@stopromannumeral
      \romannumeral\expandafter\UD@secondoftwo
      \string{\UD@CheckWhetherLeadingExplicitSpaceB.#1 }{}%
    }{}%
  }%
}%
\@ifdefinable\UD@CheckWhetherLeadingExplicitSpaceB{%
  \long\def\UD@CheckWhetherLeadingExplicitSpaceB#1 {%
    \expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo{}#1}%
    {\UD@Exchange{\UD@firstoftwo}}{\UD@Exchange{\UD@secondoftwo}}%
    {\expandafter\expandafter\expandafter\UD@stopromannumeral
     \expandafter\expandafter\expandafter}%
     \expandafter\UD@secondoftwo\expandafter{\string}%
  }%
}%
%%-----------------------------------------------------------------------------
%% Extract first inner undelimited argument:
%%
%%   \UD@ExtractFirstArgUnbraced{ABCDE} yields  A
%%
%%   \UD@ExtractFirstArgUnbraced{{AB}CDE} yields  AB
%%
%% Due to \romannumeral-expansion the result is delivered after two 
%% expansion-steps/after "hitting" \ExtractFirstArgUnbraced with 
%% \expandafter twice.
%%
%% \UD@ExtractFirstArgUnbraced's argument must not be blank.
%%
%% Use frozen-\relax as delimiter for speeding things up.
%% I chose frozen-\relax because David Carlisle pointed out in
%% <https://tex.stackexchange.com/a/578877>
%% that frozen-\relax cannot be (re)defined in terms of \outer and cannot be
%% affected by \uppercase/\lowercase.
%%
%% \ExtractFirstArgUnbraced's argument may contain frozen-\relax:
%% The only effect is that internally more iterations are needed for
%% obtaining the result.
%%
%%.............................................................................
\@ifdefinable\UD@RemoveTillFrozenrelax{%
  \expandafter\expandafter\expandafter\UD@Exchange
  \expandafter\expandafter\expandafter{%
  \expandafter\expandafter\ifnum0=0\fi}%
  {\long\def\UD@RemoveTillFrozenrelax#1#2}{{#1}}%
}%
\expandafter\UD@PassFirstToSecond\expandafter{%
  \romannumeral\expandafter
  \UD@PassFirstToSecond\expandafter{\romannumeral
    \expandafter\expandafter\expandafter\UD@Exchange
    \expandafter\expandafter\expandafter{%
    \expandafter\expandafter\ifnum0=0\fi}{\UD@stopromannumeral#1{}}%
  }{%
    \UD@stopromannumeral\romannumeral\UD@ExtractFirstArgUnbracedLoop
  }%
}{%
  \newcommand\UD@ExtractFirstArgUnbraced[1]%
}%
\newcommand\UD@ExtractFirstArgUnbracedLoop[1]{%
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo{}#1}%
  {\expandafter\UD@stopromannumeral\UD@secondoftwo{}#1}%
  {\expandafter\UD@ExtractFirstArgUnbracedLoop
   \expandafter{\UD@RemoveTillFrozenrelax#1}}%
}%
%%=============================================================================
%% \UDExtractFirstBraceGroup
%%   {<tokens in case there is no brace group>}%
%%   {<argument where to extract content of 1st brace group>}%
%%
%% In case <argument where to extract content of 1st brace group> does not
%% contain a brace group you get <tokens in case there is no brace group>.
%% Otherwise you get the content of the 1st brace group of the <argument where
%% to extract content of 1st brace group>.
%% 
%% Due to \romannumeral-expansion the result can be obtained by triggering
%% two expansions on \UDExtractFirstBraceGroup
%%
%%=============================================================================
\newcommand\UDExtractFirstBraceGroup[2]{%
  \romannumeral\UD@ExtractFirstBraceGroupLoop{#2}{#1}%
}%
\newcommand\UD@ExtractFirstBraceGroupLoop[2]{%
  \UD@CheckWhetherNull{#1}{\UD@stopromannumeral#2}{%
    \UD@CheckWhetherBrace{#1}{%
      \expandafter\UD@firstoftwo\expandafter{\expandafter}%
      \UD@ExtractFirstArgUnbraced{#1}%
    }{%
      \UD@CheckWhetherLeadingExplicitSpace{#1}{%
        \expandafter\UD@ExtractFirstBraceGroupLoop\expandafter{\UD@removespace#1}%
      }{%
        \expandafter\UD@ExtractFirstBraceGroupLoop\expandafter{\UD@firstoftwo{}#1}%
      }%
      {#2}%
    }%
  }%
}%
%%=============================================================================
\makeatother

\documentclass{article}

\begin{document}

\ttfamily\frenchspacing

\UDExtractFirstBraceGroup{%
  Hey, there is no brace group in the string!!!%
}{%
  This not {This is the string I want} This not {This not} This not {This not}%
}%

\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\foo
\expandafter\expandafter\expandafter{%
  \UDExtractFirstBraceGroup{%
    Hey, there is no brace group in the string!!!%
  }{%
    This not {This is the string I want} This not {This not} This not {This not}%
  }%
}%
\string\foo\space is: \meaning\foo

\edef\bar{%
  \UDExtractFirstBraceGroup{%
    Hey, there is no brace group in the string!!!%
  }{%
    This not {This is the string I want} This not {This not} This not {This not}%
  }%
}%
\string\bar\space is: \meaning\bar

\bigskip
\hrule
\bigskip

\UDExtractFirstBraceGroup{%
  Hey, there is no brace group in the string!!!%
}{%
  {This is the string I want} This not%
}%

\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\bas
\expandafter\expandafter\expandafter{%
  \UDExtractFirstBraceGroup{%
    Hey, there is no brace group in the string!!!%
  }{%
    {This is the string I want} This not%
  }%
}%
\string\bas\space is: \meaning\bas

\edef\bat{%
  \UDExtractFirstBraceGroup{%
    Hey, there is no brace group in the string!!!%
  }{%
    {This is the string I want} This not%
  }%
}%
\string\bat\space is: \meaning\bat

\bigskip
\hrule
\bigskip

\UDExtractFirstBraceGroup{%
  Hey, there is no brace group in the string!!!%
}{%
  This is a string without brace group.%
}%

\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\bau
\expandafter\expandafter\expandafter{%
  \UDExtractFirstBraceGroup{%
    Hey, there is no brace group in the string!!!%
  }{%
    This is a string without brace group.%
  }%
}%
\string\bau\space is: \meaning\bau

\edef\bav{%
  \UDExtractFirstBraceGroup{%
    Hey, there is no brace group in the string!!!%
  }{%
    This is a string without brace group.%
  }%
}%
\string\bav\space is: \meaning\bav

\end{document}

在此处输入图片描述

相关内容