English 中文(简体)
2. 搜索档案得到回馈,获得与模式相符的价值
原标题:Search files recursively and get the value matching the pattern
  • 时间:2012-04-19 14:59:16
  •  标签:
  • regex
  • grep

因此,我需要一些青少年工具(没有外来语气)或实验室功能,其作用如下。 (我不想写一下我自己的休养职能,例如使用文档_get_contents())

I have a dir with several sub-dirs. Some of the file contain something like <?php ech $this->translate( string ); ?> whereas string is always different.

怎样才能形成一种与上述职能相匹配的模式,而只是让我回来呢? 晚上,我看着 gr子和ec子;我看着他的str子。 但也许会做得更好。

My grep example: grep -r “$this[->]translate(* )” dir

无问题: 如何做到这一点?

最佳回答

You can get just the string with lookahead and lookbehind assertions: grep -rhoP "(?<=this->translate( )[^ ]*(?= ))" dir

问题回答

Starting with your little question:

如何做到这一点?

The second example will not search inside child directories of dir starting with a . (because they won t be matched by the glob). So if you a directory that looks like this:

dir/
  .a_dotted_dir/
  a_visible_dir/

第一个例子将在<代码>.a_dotted_dir内查询。 而第二个例子则不会。

怎样才能形成一种与上述职能相匹配的模式,而只是让我回来呢?

You can t, with grep by itself. Grep just matches patterns but does not perform any sort of processing on them. You can probably accomplish what you want by listing files that match your search pattern and processing them with sed, something like:

grep -rl "this->translate( .* )" dir |
  xargs sed -n "s/.*this->translate( ([^ ]*) ).*/1/p"

此外,我注意到,我刚才对你的定期表达作了一些改动。 <代码>[->]与->不相匹配,并将$<>ts置于双引文内,最终将用空示取代“密码>。 您需要放弃<条码><美元/代码>、逃避或使用单一报价。

  1. 采用方括号界定了特性类别,而美元签字则将按壳体作为变量加以解释。 你们必须逃脱,以达到正确的含义。 我不妨这样做:

    echo "<?php echo $this->translate( string ); ?>" 
      | grep -ho "$thiss*->s*translate( .*? )" 
      | sed "s/$this->translate( (.*) )/1/"
    

    页: 1

  2. <代码>grep -r <pattern> dir和grep -r <pattern> dir/*之间的区别有些微妙。

    你在指挥线上操作<条码>grep -r <pattern> dir/*时,实际发生的情况是: ll将把<条码>dir/*的论点列入名录。

    实质上:

    grep -rho <pat> dir/*
    

    相当于:

    grep -rho <pat> dir/a dir/b dir/c [etc...]
    

    a、b和c为目录/档案。 这样,这些名录的每个目录和dir上的所有档案将重新封入,但不会有文件或印本。

    更不用<代码>grep -rho <pat> dir,其中考虑到所有档案。





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...