开发者

Convert Javascript RegExp to work with Grep or Sed

开发者 https://www.devze.com 2023-04-05 06:44 出处:网络
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:

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

精彩评论

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