Skip to content

Instantly share code, notes, and snippets.

@hhatto
Created September 18, 2010 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhatto/585479 to your computer and use it in GitHub Desktop.
Save hhatto/585479 to your computer and use it in GitHub Desktop.
patch of grin force word regex match
Index: grin.py
===================================================================
--- grin.py (revision 80)
+++ grin.py (working copy)
@@ -762,6 +762,8 @@
help="show program's version number and exit")
parser.add_argument('-i', '--ignore-case', action='append_const',
dest='re_flags', const=re.I, default=[], help="ignore case in the regex")
+ parser.add_argument('-w', '--word-regexp', action='store_true', default=False,
+ dest='is_matchword', help="force regex to match only whole words")
parser.add_argument('-A', '--after-context', default=0, type=int,
help="the number of lines of context to show after the match [default=%(default)r]")
parser.add_argument('-B', '--before-context', default=0, type=int,
@@ -1003,7 +1005,11 @@
flags = 0
for flag in args.re_flags:
flags |= flag
- return re.compile(args.regex, flags)
+ if args.is_matchword:
+ regex = "\\b(%s)\\b" % args.regex
+ else:
+ regex = args.regex
+ return re.compile(regex, flags)
def grin_main(argv=None):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment