REGEX check only when start of line

你说的曾经没有我的故事 提交于 2019-12-12 03:37:03

问题


Suppose we want to keep the entire line of a string only if a particular word say e.g 'test' appears at starting of line.

If it appears anywhere then the entire line should be removed

e.g

if function_test()=5; //here this entire line should be removed

test sample =5; //here this entire should be there

回答1:


From Oracle 10g R2 on you should be able to use the anchor \A to require the match at the beginning of the string (will only work for single-line strings thus).

http://www.regular-expressions.info/oracle.html




回答2:


What do you mean by keep / remove lines? Where is this regex supposed to run? I.e. is it a part of an SQL command, or part of a grep, or sg else?

Regarding SQL you can use LIKE operator:

WHERE line LIKE 'test%'

You can use substring too:

WHERE substring(line, 1, 4) = 'test'

Using grep or any other language, you can specify start of line, e.g.:

grep '^test' bigfile.txt



回答3:


Try...

...
WHEN REGEXP_LIKE(string,'^test','i') THEN
     //this is a good line, do what you want or return string;
END
...


来源:https://stackoverflow.com/questions/43187038/regex-check-only-when-start-of-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!