3

一致する正規表現が必要です:

  • a)特定の単語の小文字/大文字のすべての組み合わせ
  • b)いくつかのケースの組み合わせを除いて。

何千ものソース コード ファイル、スペルミスのある変数bashの出現箇所を検索する必要があります。

具体的には、私が探している単語はFrontEnd、コーディング スタイル ガイドで、コンテキストに応じて 2 つの方法で正確に記述できるものです。

FrontEnd (F and E upper)
frontend (all lower)

したがって、次のように、コーディング標準に従っていない発生を「キャッチ」する必要があります。

frontEnd
FRONTEND
fRonTenD

私はこの特定の例について正規表現の多くのチュートリアルを読んでいますが、「このパターンに一致するが、これまたは他のパターンである場合は一致しない」と言う方法が見つかりません。

「000000 から 999999 までの任意の数値 (正確には 555555 または数値 123456 を除く)」と一致させようとするのと似ていると思いますが、論理は似ていると思います (もちろん、これを行うために結び目を付けません :) )

thnx


追加コメント:

行を見逃す可能性があるため、grepパイプを使用できません。grep -vたとえば、次のようにします。

grep -i frontend | grep -v FrontEnd | grep -v frontend

次のような行を見逃すでしょう:

if( frontEnd.name == 'hello' || FrontEnd.value == 3 )

2 番目のオカレンスは行全体を非表示にするためです。egrepしたがって、必要な正確な一致を行うことができる正規表現を探しています。

4

2 に答える 2

1

egrep先読みをサポートしていないため、これを簡単に行うことはできません。perl でこれを行うのがおそらく最も簡単です。

perl -ne 'print if /(?!frontend|FrontEnd)(?i)frontend/;'

使用するには、テキストをパイプするだけですstdin

仕組み:

perl -ne 'print if /(?!frontend|FrontEnd)(?i)frontend/;'
^     ^^  ^     ^  ^ ^ ^                 ^   ^ The pattern that matches both the correct and incorrect versions.
|     ||  |     |  | | |                 | This switch turns on case insensitive matching for the rest of the regular expression (use (?-i) to turn it off) (perl specific)
|     ||  |     |  | | | The pattern that match the correct versions.
|     ||  |     |  | | Negative forward look ahead, ensures that the good stuff won't be matched
|     ||  |     |  | Begin regular expression match, returns true if match
|     ||  |     | Begin if statement, this expression uses perl's reverse if semantics (expression1 if expression2;)
|     ||  | Print content of $_, which is piped in by -n flag
|     || Evaluate perl code from command line
|     | Wrap code in while (<>) { } takes each line from stdin and puts it in $_
| Perl command, love it or hate it.
于 2013-03-02T02:38:23.513 に答える
0

これはコメントのはずですが、使用できない理由はありますsedか? みたいなことを考えている

sed 's/frontend/FrontEnd/ig' input.txt

それはもちろん、逸脱したバージョンを修正したいという前提で...

于 2013-03-02T02:23:06.893 に答える