开发者

Regex to select all input between { and }

I'm trying to use regex for selecting all characters and words between the two signs: { and } I want to do this in order to translate some php tpl files using OmegaT or Trados.

I'm sure this is fairly simple but couldn't make it on my own, can someone help?

Example:

{if $smarty.session.id_admin && $sm.admin_panel}

all this line needs to be selected and tagged (for segme开发者_StackOverflow中文版ntation purposes in OmegaT)


"\{(.+)\}"

That's all you need.

\{ is for matching { character and \ is escape character because { has special meaning in regex.

. means every character

+ means one or more occurrence of previous

\} is for matching } character and \ is escape character because } has special meaning in regex.

() as @Gabi said is for grouping so that you can remember what matched your regex.


If you will be matching multiple sets of things in the format { stuff }, you'll want to make sure you don't grab a } inside your match:

/{([^}]*)}/

[^}] means match any character except }.

If you want to match across lines you'll need to add the s option:

/{([^}]*)}/s

This would work things like:

{ first one } and { second 
one }

If you need to support nested sets of { stuff } (e.g. { a { b } }, then you're out of luck.


/\{(.*)\}/ will do the job in PHP's preg_match() which you can test here although you will need a different method to globally capture each occurance (ie. preg_match_all())


{\(.*\)}

OR (in some editors/tools):

\{\(.*\)\}


Try this:

\{([^}]*)\}

Looks for a literal '{' followed by any number of characters that are not '}' followed by a closing literal '}'.


This would actually require more work than you think. I am not a php user, but if you
intend to grab the contents of 'blocks' you may have to take into account nesting,
escaped {} inside strings even regular expressions. Probably you would want a php token
parser for the job.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜