在替换之前处理正则表达式匹配

发布于 2024-10-12 02:41:11 字数 569 浏览 8 评论 0原文

我正在寻找应用程序和编程语言结构来搜索正则表达式模式,以某种方式转换匹配,然后替换它。一个非常简单的示例:将“myCamelCasedString”转换为“my_camel_cased_string”。

在 Ruby 中,它简单而简洁:

 s = "myCamelCasedString".gsub(/[A-Z]/) { |m| "_" + m.downcase }

在 PHP 中,它更长,但也是可能的。

  preg_replace_callback('/[A-Z]/', 
     // Using PHP 5.3 anonymous function as callback
     function($m) { return "_" . strtolower($m[0]); }, 
    "myCamelCasedString");

文本编辑器 jEdit 也通过“Beanshell 片段”支持这一点,但我总是必须查找如何做到这一点。那么 - 我将如何在其他语言中执行此操作,是否有专用的应用程序/编辑器可以让我执行此操作(以及可能的转换的方便参考)?

I'm looking for applications and programming language constructs to search for a regular expression pattern, transform the match in some way and then replace it. A very simple example: Transforming "myCamelCasedString" to "my_camel_cased_string".

In Ruby it's easy and concise:

 s = "myCamelCasedString".gsub(/[A-Z]/) { |m| "_" + m.downcase }

In PHP it's longer, but also possible

  preg_replace_callback('/[A-Z]/', 
     // Using PHP 5.3 anonymous function as callback
     function($m) { return "_" . strtolower($m[0]); }, 
    "myCamelCasedString");

The text editor jEdit also supports this through a "Beanshell snippet" but I always have to look up how to do it. So - how would I do this in other languages and is there a dedicated application/editor that lets me do this (together with a handy reference of possible transformations)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨光如昨 2024-10-19 02:41:11

我认为 Ruby 是您正在寻找的专用应用程序:

数据:

some other text
myCamelCasedString
here is yetAnotherCamelCasedString

scriptlet:

$ ruby -pe '$_.gsub!(/[A-Z]/) { |m| "_" + m.downcase }' <input
some other text
my_camel_cased_string
here is yet_another_camel_cased_string

神奇的酱汁是“-p”开关。它将“-e”开关提供的代码包装在“while gets (); ... ; print $_ end”中。 '$_' 是一个 Perlish 变量,保存最近读取的行。

I think Ruby is the dedicated application you're looking for:

The data:

some other text
myCamelCasedString
here is yetAnotherCamelCasedString

The scriptlet:

$ ruby -pe '$_.gsub!(/[A-Z]/) { |m| "_" + m.downcase }' <input
some other text
my_camel_cased_string
here is yet_another_camel_cased_string

The magic sauce is the "-p" switch. It wraps the code provided with the "-e" switch in "while gets (); ... ; print $_ end". '$_' is a Perlish variable which holds the most recently read line.

初吻给了烟 2024-10-19 02:41:11

在 Perl 中:

$str =~ s/([A-Z])/'_'.lc$1/eg;

In Perl:

$str =~ s/([A-Z])/'_'.lc$1/eg;
寂寞花火° 2024-10-19 02:41:11

由于我希望用尽可能多的编程语言来回答这个问题,因此这里是 JavaScript 解决方案:

s = "myCamelCasedString".replace(/[A-Z]/g, function(s) { 
  return '_'+s.toLowerCase();
});

请参阅 来自 MDC 的文档 了解有关函数参数的详细信息。您将获得子匹配和匹配偏移量作为附加参数。


这是Python解决方案:

import re
re.sub(r"[A-Z]", lambda s: "_" + s.group(0).lower(), "myCamelCasedString")

Since I want this question answered in as many programming languages as possible, here is the JavaScript solution:

s = "myCamelCasedString".replace(/[A-Z]/g, function(s) { 
  return '_'+s.toLowerCase();
});

See the documentation from MDC for details on the function parameters. You get submatches and the match offset as additional parameters.


Here is the Python solution:

import re
re.sub(r"[A-Z]", lambda s: "_" + s.group(0).lower(), "myCamelCasedString")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文