Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex使用ruby从文件中提取方法名_Ruby_Regex - Fatal编程技术网

Regex使用ruby从文件中提取方法名

Regex使用ruby从文件中提取方法名,ruby,regex,Ruby,Regex,我必须在很多文件中进行一些更改,因此我正在编写一个Ruby脚本,它将打开所有这些文件并进行更改。我已经解决了所有问题,但只需要1Regex就可以找到所有带有特定注释的方法,因为这些文件来自Java 文件的示例部分: ... @Test(priority = 10) @RunAsClient public void methodName(){ ... } ... @Test(priority = 20) public void otherMethodName(){ ... }

我必须在很多文件中进行一些更改,因此我正在编写一个
Ruby
脚本,它将打开所有这些文件并进行更改。我已经解决了所有问题,但只需要1
Regex
就可以找到所有带有特定注释的方法,因为这些文件来自Java

文件的示例部分:

...

@Test(priority = 10)
@RunAsClient
public void methodName(){
   ...
}

...

@Test(priority = 20)
public void otherMethodName(){
   ...
}

...
我需要找到所有这些有这种注释的方法。 结果应该是:

str.scan(/(@Test\(priority = \d{2}\)\n)?(@RunAsClient\n)?\w+\s+(\w+\s+)?(\w+)/).map(&:last)
result=['methodName','otherMethodName']

我可以提取其中一个,但不能同时提取两个。为了提取第一种,我创建了一个正则表达式

first_regex_pattern=/(?
关于所要求的结果:

str.scan(/(@Test\(priority = \d{2}\)\n)?(@RunAsClient\n)?(\w+\s+\w+\s+\w+)/)
   .map(&:last)
   .map { |e| e.split(' ').last }                         
#⇒ [
#  [0] "metodName",
#  [1] "otherMethodName"
# ]

mudasobwa的答案看起来不错,但如果您的函数可以只有一个类型,而没有关键字
public
,则正则表达式应该是:

str.scan(/(@Test\(priority = \d{2}\)\n)?(@RunAsClient\n)?\w+\s+(\w+\s+)?(\w+)/).map(&:last)
这也有点短

如果您只对函数名感兴趣,而对上下文行中的数据不感兴趣,那么这个正则表达式要简单得多——它匹配函数定义的签名:

 str.scan(/\w+\s+(\w+\s+)?(\w+)\(\)\s?\{/).map(&:last)
这将查找类型、方法名和
()
,后跟可选空格,然后是
{

str==1次
str =<<_
@Test(priority =10)
@RunAsClient
public void metodName(){
   ...
}

   ...

@Test(priority =     20)
public void otherMethodName(){
   ...
}
...
_

r = /
    \@Test\(\s*priority\s*=\s*\d+\s*\)\s*\n  # Match string
    (?:\@RunAsClient\n)?  # Optionally match string
    (?:\w+\s+)+           # Match (word, >= 1 spaces) >= 1 times
    \K                    # Forget everything matched so far
    \w+                   # match word
    (?=                   # begin positive lookahead
      (?:\([^)]*\)\s*\{)  # match paren-enclosed expression, >= 0 spaces, {
      |                   # or
      (?:\s*\{)           # match >= 0 spaces, {
    )                     # end positive lookahead
    /x                    # extended/free-spacing regex definition mode

str.scan r
  #=> ["metodName", "otherMethodName"]
\K#忘了所有匹配的东西吧 \w+#匹配单词 (?=#开始积极前瞻 (?:\([^)]*\)\s*\{)匹配paren封闭表达式,>=0个空格{ |#或 (?:\s*\{)匹配>=0个空格{ )#结束正向前瞻 /x#扩展/自由间距正则表达式定义模式 str.scan r #=>[“metodName”、“otherMethodName”]
有一个通用的(缩写)word regex和Ruby class
Regexp
,但是Ruby中没有
regex
这样的东西。这可能是一个有用的脚本,可以通过编程方式更改多个文件:--它不是Ruby,但工作得很好,为什么不使用Java注释处理库、Java IDE或编程Java重构工具呢了解Java代码和Java注释?如果
r
是您的第一个正则表达式,
“生活是美好的”。扫描r#=>[“美好”]
。第二个可能与
@Bugaboo
行后面的一行中的字符串匹配,而不是
@Test…
@RunAsClient
,我相信OP希望避免这些。似乎有一个问题:
“生活是美好的”。扫描/(@Test\(优先级=\d{2})\n)?(@RunAsClient\n)?(\w+\s+\w+\s+\s+\s+\w+\s+\w+)/[[nil,“生活是美好的”]
@CarySwoveland当然,这种方法有很多小故障;我假设输入是严格定义的,我回答了“如何组合我的解决方案”的问题