regex - Perl, Change the Case of Letter at { character
问题描述
I am a perl newb, and just need to get something done quick and dirty. I have lines of text (from .bib files) such as
Title = {{the Particle Swarm - Explosion, Stability, and Convergence in a Multidimensional Complex Space}},
How can I write a regex such that the first letter after the second { becomes capitalised.
Thanks
解决方案
One way, for the question as asked
$string =~ s/{{\K(\w)/uc($1)/ge;
whereby /e
makes it evaluate the replacement side as code. The \K
makes it drop all previous matches so {{
aren't "consumed" (and thus need not be retyped in the replacement side).
If you wish to allow for possible spaces: $string =~ s/{{\s*\K(\w)/uc($1)/ge;
, and as far as I know bibtex why not allow for spaces between curlies as well, so {\s*{
.
If simple capitalization is all you need then \U$1
in the replacement side sufficies and there is no need for /e
modifier with it, per comment by Grinnz. The \U
is a generic quote-like operator, which can thus also be used in regex; see under Escape sequences in perlre, and in perlretut.
I recommend a good read through the tutorial perlretut. That will go a long way.
However, I must also ask: Are you certain that you may indeed just unleash that on your whole file? Will it catch all cases you need? Will it not clip something else you didn't mean to?
推荐阅读
- mysql - MySql中只有输入标记时如何解析单词?
- javascript - QlikSense Field selectValues API - 当字段名称是混搭应用程序中的日期数据类型时不起作用
- json - React Native - NSNumber 类型的 JSON 值“1311”无法转换为 NSDictionary
- google-apps-script - 我无法在具有编辑权限的 Google 表格上创建脚本
- python - TensorFlow:没有名为 Pandas 的模块(我已经有了 Pandas)
- azure - Azure Cosmos DB SQL API
- c# - RSA-SHA256 签名不匹配
- flutter - Flutter 和 GetX - 当我更新我的 RxObject 时,小部件不会重绘
- javascript - antd 图片上传需要将基于类的组件转换成react -hooks,无法增加图片的宽高
- clang - 如何构建clang-tools?“make clang-tools”什么都不做