

If it is a GNU grep, you could use git grep 'buildLabel("\+")' (not sure it works with git). You can use git grep 'buildLabel("\")' in POSIX BRE though. In git grep 'buildLabel("+")', you are using the POSIX BRE regex, and + is parsed as a literal + char, not as a one or more quantifier. it should be git grep -P "buildLabel\(\"\w+\"\)". In your git grep -P "buildLabel(\"\w+\")", the parentheses must be escaped in order to be matched as literal parentheses, i.e. You can enable PCRE regex syntax with -P option, and in that case you should refer to PCRE documentation. Use fixed strings for patterns (don’t interpret pattern as a regex). To make git grep treat the pattern as a fixed string you need -F: -F So, by default, git grep treats the pattern string as a POSIX BRE regex, not as a fixed string. Use POSIX extended/basic regexp for patterns. So what am I doing wrong with git grep? Or is it broken?įYI: I am using git version 2.35.1 from Homebrew on macOS Big Sur. $ git grep 'buildLabel\("*"\)' (in case + isn't implemented) Thanks to commenting that with PCRE the parens need to be escaped (I am always confused by that). I also tried changing the quote characters and got the same results.
#Using grep regex code#
That returned the instance in the code that I already knew existed. I verified that I could search with a fixed string. So I tried it without the Perl extension. I am very familiar with regular expressions and I see that git grep supports Perl character classes. The correct call should be: JLabel label = buildLabel(getString("Alphabet")) So I would like to search my code for uses of these methods without a lookup for the localized string. There are also buildBoldLabel(), buildMultiLineLabel(), and buildTextArea(). In this case buildLabel() is an inherited utility method. For example: JLabel label = buildLabel("Alphabet") I have places in the code with non-localized strings. g).I have used git grep for years to search for fixed strings and haven't used it much for doing regular expression searches. In the above, the files are matched because their names contain 2 characters followed by "g".
#Using grep regex update#
Update for OP: Example to find files that start with 2 characters (the dots "." means 1 char) followed by "g" using regex for extended globbing, see here and some simple examples here. Regex are not supported for version of bash <3.2 (as dennis mentioned), but you can still use extended globbing (by setting extglob ). Regex are more versatile and "convenient" than "glob patterns", however unless you are doing complex tasks that "globbing/extended globbing" cannot provide easily, then there's no need to use regex. In bash, when to use glob pattern and when to use regular expression? Thanks! # looking for the word "grid" in the string $ggĬase "$gg" in ?grid*) echo "found" esacĬase "$gg" in s?grid*) echo "found" esac In your examples, you can use case/esac to look for strings patterns. you an use it to compare strings as well. No, "glob" pattern is not only used for file names. Glob pettern not only used for file names?
