开发者

Convert Javascript RegExp to work with Grep or Sed

I needed to grep some files on a server so I quickly hacked out and tested a regexp in a javascript console to meet my needs:

var regexp = /mssql_query\s*\([\"\'][a-z0-9_\s]*(_sp|usp_)/i

// want to pass
regexp.test('mssql_query ("something_sp')
regexp.test('mssql_query("exec something_sp')
regexp.test("mssql_query('something_else_sp")
regexp.test('mssql_query("_usp_sp')
regexp.test('mssql_query ("_usp_somethig')
regexp.test("mssql_query('_usp_something_else")

// want to fail
regexp.test('mssql_query ("something_s')
regexp.test('mssql_query("exec something_p')
regexp.test('mssql_query("select')

The expres开发者_运维技巧sion works perfectly for all the test cases I threw at it, however I can't seem to get the expression to work with grep. Is there a way to convert an EMCA expression to an ERE or BRE expression so I can use it with grep or sed?

I've tried tweaking it to work in grep but have failed miserably.


use egrep (alias for grep -E , using extended regular expressions).

also instead of the /i modifier use the -i flag for grep.


grep "mssql_query *([\"\'][a-z0-9_ ]*_sp\|usp_" 

ought to do the job. It is looking for:

  1. mssql_query, then
  2. 0 or more spaces (that's the " *"), then
  3. (, then " or ' (that's the ([\"\']), then
  4. 0 or more characters that are lowercase letters, numbers, underscores or spaces (that's the [a-z0-9_ ]*), then
  5. _sp or usp_ (that's the _sp\|usp_)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜