regex

awk print matching line and line before the matched

六月ゝ 毕业季﹏ 提交于 2021-02-07 03:18:15
问题 Following is what I am trying to do using awk. Get the line that matches the regex and the line immediately before the matched and print. I can get the line that matched the regex but not the line immediately before that: awk '{if ($0!~/^CGCGGCTGCTGG/) print $0}' 回答1: In this case you could easily solve it with grep: grep -B1 foo file However, if you need to to use awk: awk '/foo/{if (a && a !~ /foo/) print a; print} {a=$0}' file 回答2: /abc/{if(a!="")print a;print;a="";next} {a=$0} 回答3: use

awk print matching line and line before the matched

时光总嘲笑我的痴心妄想 提交于 2021-02-07 03:05:57
问题 Following is what I am trying to do using awk. Get the line that matches the regex and the line immediately before the matched and print. I can get the line that matched the regex but not the line immediately before that: awk '{if ($0!~/^CGCGGCTGCTGG/) print $0}' 回答1: In this case you could easily solve it with grep: grep -B1 foo file However, if you need to to use awk: awk '/foo/{if (a && a !~ /foo/) print a; print} {a=$0}' file 回答2: /abc/{if(a!="")print a;print;a="";next} {a=$0} 回答3: use

awk print matching line and line before the matched

Deadly 提交于 2021-02-07 03:05:23
问题 Following is what I am trying to do using awk. Get the line that matches the regex and the line immediately before the matched and print. I can get the line that matched the regex but not the line immediately before that: awk '{if ($0!~/^CGCGGCTGCTGG/) print $0}' 回答1: In this case you could easily solve it with grep: grep -B1 foo file However, if you need to to use awk: awk '/foo/{if (a && a !~ /foo/) print a; print} {a=$0}' file 回答2: /abc/{if(a!="")print a;print;a="";next} {a=$0} 回答3: use

Combining multiple regex substitutions

陌路散爱 提交于 2021-02-07 02:42:12
问题 I'm trying to delete some things from a block of text using regex. I have all of my patterns ready, but I can't seem to be able to remove two (or more) that overlap. For example: import re r1 = r'I am' r2 = r'am foo' text = 'I am foo' re.sub(r1, '', text) # Returns ' foo' re.sub(r2, '', text) # Returns 'I ' How do I replace both of the occurrences simultaneously and end up with an empty string? I ended up using a slightly modified version of Ned Batchelder's answer: def clean(self, text):

Python regex to match punctuation at end of string

假装没事ソ 提交于 2021-02-07 01:59:15
问题 I need to match if a sentence starts with a capital and ends with [?.!] in Python. EDIT It must have [?.!] only at end but allow other punctuation in the sentence import re s = ['This sentence is correct.','This sentence is not correct', 'Something is !wrong! here.','"This is an example of *correct* sentence."'] # What I tried so for is: for i in s: print(re.match('^[A-Z][?.!]$', i) is not None) It does not work, after some changes I know the ^[A-Z] part is correct but matching the

Python regex to match punctuation at end of string

爱⌒轻易说出口 提交于 2021-02-07 01:55:41
问题 I need to match if a sentence starts with a capital and ends with [?.!] in Python. EDIT It must have [?.!] only at end but allow other punctuation in the sentence import re s = ['This sentence is correct.','This sentence is not correct', 'Something is !wrong! here.','"This is an example of *correct* sentence."'] # What I tried so for is: for i in s: print(re.match('^[A-Z][?.!]$', i) is not None) It does not work, after some changes I know the ^[A-Z] part is correct but matching the

Python regex to match punctuation at end of string

♀尐吖头ヾ 提交于 2021-02-07 01:54:02
问题 I need to match if a sentence starts with a capital and ends with [?.!] in Python. EDIT It must have [?.!] only at end but allow other punctuation in the sentence import re s = ['This sentence is correct.','This sentence is not correct', 'Something is !wrong! here.','"This is an example of *correct* sentence."'] # What I tried so for is: for i in s: print(re.match('^[A-Z][?.!]$', i) is not None) It does not work, after some changes I know the ^[A-Z] part is correct but matching the

Python regex to match punctuation at end of string

前提是你 提交于 2021-02-07 01:53:56
问题 I need to match if a sentence starts with a capital and ends with [?.!] in Python. EDIT It must have [?.!] only at end but allow other punctuation in the sentence import re s = ['This sentence is correct.','This sentence is not correct', 'Something is !wrong! here.','"This is an example of *correct* sentence."'] # What I tried so for is: for i in s: print(re.match('^[A-Z][?.!]$', i) is not None) It does not work, after some changes I know the ^[A-Z] part is correct but matching the

Python regex to match punctuation at end of string

喜夏-厌秋 提交于 2021-02-07 01:53:51
问题 I need to match if a sentence starts with a capital and ends with [?.!] in Python. EDIT It must have [?.!] only at end but allow other punctuation in the sentence import re s = ['This sentence is correct.','This sentence is not correct', 'Something is !wrong! here.','"This is an example of *correct* sentence."'] # What I tried so for is: for i in s: print(re.match('^[A-Z][?.!]$', i) is not None) It does not work, after some changes I know the ^[A-Z] part is correct but matching the

JMeter Proxy exclusion patterns still being recorded

女生的网名这么多〃 提交于 2021-02-06 15:00:20
问题 I am using JMeter to record traffic in my browser. In my URL Patterns to Exclude are: .*\.jpg , .*\.js , .*\.png Which looks like they should block these patterns (I've even tested it with a regex tester here) Yet, I still see plenty of these files get pulled up. In a related forum someone had a similar issue, but his was caused by having additional url parameters afterwards (eg www.website.com/image.jpg?asdf=thisdoesntmatch). However this doesn't seem to be the case here. Can anyone point me