Using regex to replace everything but a specific line - regex

I am trying to replace everything that does not match the specified string. This would be I wish I could to a Match or something like that but I am sadly working in a software that only allows me to do regex calls within their replace function
For example, my string will be formatted like this:
HTTP/1.1 200 OK
Date: Wed, 12 Dec 2018 20:51:23 GMT
Server: Apache
x-request-id: eb6cf28d-fad9-42e2-ac3d-4efcf66c5f9b
ETag: 83705a06-4562-4bbd-bcbe-c7643d2d6008
Content-Length: 531
Strict-Transport-Security: max-age=31536000;
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Robots-Tag: none
P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
Keep-Alive: timeout=15, max=200
Connection: Keep-Alive
Content-Type: application/json;charset=UTF-8
I need to remove everything that does not match
ETag: 83705a06-4562-4bbd-bcbe-c7643d2d6008
I have written some regex that selects ETag: 83705a06-4562-4bbd-bcbe-c7643d2d6008 that looks like (ETag: [a-zA-Z\d.-]*)
And this does select that line, but I need to replace everything but that line. I have read a few post that asked suggested to do the following:
^(ETag: [a-zA-Z\d.-]*)
(?:(ETag: [a-zA-Z\d.-]*))
(ETag: [a-zA-Z\d.-]*)[^]
None of those seemed to work I have a sample of this running on RegExr
Once again, I need to replace everything that does not match ETag: GUID. I would appreciate any help in doing this. I am sure I am just missing something small.

Regex doesn't really have negation, but usually you can use negative lookaheads:
^(?!ETag: [a-zA-Z\d.-]*).*$
This matches the beginning of a line that can't match ETag:..., followed by the rest of the line. You can see how it works in this demo.

Related

How to use regex_search to get the a line of text from two different .txt files

I'm trying to use this regex pattern Subject: [\a-zA-Z_0-9]+ in c++ to get this line of text from two different files one being this:
Message-ID: <..>
Subject: AAAI-22 General Information
MIME-Version: 1.0
Content-Type: multipart/alternative; ..
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
and the other being
Subject: Re: Possible need for … 240
Thread-Topic: Possible need for printout for .. 240
however, the regex pattern matches:
Subject: AAAI-22 General Information MIME-Version: 1.0
Content-Type: multipart/alternative; ..Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
for the first text file and for the second file the pattern matches
Subject: Re: Possible need for
All I want my regex pattern to search for and print out is the Subject: and whatever else is in that line and nothing else. How can I do that using regex_search and regex_replace patterns?
You can use this regex pattern to search for Subject line
"Subject: .+"
Explanation:
Subject: matches the characters Subject: literally (case
sensitive).
.matches any character (except for line terminators)
+ matches the previous token between one and unlimited times

How to create regular expression extractor in jmeter

I am trying to extract the location from a POST in jmeter using the Regex Expression Extractor. The header looks like the below
HTTP/1.1 201 Created
Transfer-Encoding: chunked
groopId: ID-99-Inc-07-12300-2650126876118-1-236
User-Agent: RestClient-Tool
Date: Thu, 14 Feb 2019 13:57:23 GMT
Location: v2/TestData/12sff-13343e-dff3444455
Content-Type: text/plain; charset=UTF-8
I need theLocation: v2/TestData/12sff-13343e-dff3444455 .
I am struggling because I am not quite sure how to create the regex I need.
Add Regular Expression Extractor as a child of the request which returns above headers
Configure it as follows:
Apply to: depending on where this Location header comes from
Field to check: Response Headers
Regular Expression: Location: (.*)
Template: $1$
That's it, you should be able to access the extracted value as ${location} where required.
References:
JMeter: Regular Expressions
Using RegEx (Regular Expression Extractor) with JMeter
Perl 5 Regex Cheat sheet

Need regex to match multiple string in HTML response

I'm trying to build a regex to match mutliple strings from an HTML response.
This is used to monitor a webpage from a load balancer. If the regex has a match, the load balancer will consider the server as UP and will send traffic.
Example of expected HTML response :
HTTP/1.1 200
X-AREQUESTID: *1KIRCWLx688x71065x0
X-XSS-Protection: 1; mode=block
X-FRAME-OPTIONS: SAMEORIGIN
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Content-Type: application/json
Transfer-Encoding: chunked
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Wed, 31 Oct 2018 11:28:14 GMT
{"state":"RUNNING"}
What I want to achieve is trying to match {"state":"RUNNING"} OR {"state":"MAINTENANCE"} AND HTTP/1.1 200
So I have the following working regexes, but I don't know how to bundle them up together ;-)
\{\"state\":\"RUNNING\"\}|\{\"state\":\"MAINTENANCE\"\}
will match {"state":"RUNNING"} OR {"state":"MAINTENANCE"}
HTTP\/1\.(0|1) (200|301|302)
will match the HTTP response code 200, 301 or 302 (ex : HTTP/1.1 200)
So now, how do I build a mega regex will all conditions ?
HTTP\/1\.(0|1) (200|301|302) AND \{\"state\":\"RUNNING\"\}|\{\"state\":\"MAINTENANCE\"\} ?
Is it even possible?
Thanks in advance
This will do the trick:
/HTTP\/1\.(0|1) (200|301|302).*?(\{\"state\":\"RUNNING\"\}|\{\"state\":\"MAINTENANCE\"\})/s
The key in this version is the s flag, which allows . to match a newline (\n) character. Demo on regex101.
Another option, without using flags:
HTTP\/1\.(0|1) (200|301|302)[\s\S]*?(\{\"state\":\"RUNNING\"\}|\{\"state\":\"MAINTENANCE\"\})
The key is the [\s\S]*?, which matches literally anything (new lines included) in a non-greedy fashion. Demo on regex101.
Combining two regular expressions is done by simply concatenating them. If you want to allow arbitrary characters in between, use .* between them.
Assuming that your regular expressions match over the whole response including headers and body and not just single lines, the following should suffice.
HTTP\/1\.(0|1) (200|301|302)\r\n(.*?)(\{\"state\":\"RUNNING\"\}|\{\"state\":\"MAINTENANCE\"\})$

Unable to extract GUID from URL within header with regular expression extraction

I'm having issue with extracting GUID from location in header. I tried following and it doesn't seems to extract at all.
Response headers:
HTTP/1.1 201 Created
Content-Length: 99
Content-Type: application/json; charset=utf-8
Location: https://hello.com/books/category/cdeacb91af9f4faca842714c4ee9be45
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Thu, 05 Mar 2015 22:29:37 GMT
I tried with regular expression and it just won't extract that GUID. In most scenarios, I was able to extract whatever I want with (.+?)
Location: https://hello.com/books/category/(?s)(.*?)$
Location: https://hello.com/books/category/(.+?)
Thanks.
You can simply use a character class:
Location: https://hello.com/books/category/([0-9a-fA-F]{32})
[0-9a-fA-F] means all digits and letters between A and F, both upper and lowercase.
Your (.+?) regex will work fine if there will be right boundary provided.
Try changing it to
Location: https://hello.com/books/category/(.*)
It should do the trick
Also don't forget to change "Field to check" to "Response Headers" as regex will fail to lookup the "Location" header in the response body.
See Using RegEx (Regular Expression Extractor) with JMeter guide for more details.

Extracting from Response Header with Regular Expression

I'm trying to extract the confirmation number at the end of the location tag in the response header to a page using RegEx. The response header is as follows:
HTTP/1.1 302 Moved Temporarily
Date: Mon, 09 Sep 2013 17:55:50 GMT
Server: Apache-Coyote/1.1
Location: http://test.regtest.com/cart/confirmation?confirmationNumber=00284031
Content-Language: en
Content-Length: 0
X-Cnection: close
Content-Type: text/plain; charset=UTF-8
Vary: Accept-Encoding
For instance, if in the header the line is this:
Location: http://test.regtest.com/cart/confirmation?confirmationNumber=00284031
I am looking to return this to use as a variable later:
00284031
My current RegEx expression is something like this:
Location: http://test.regtest.com/cart/confirmation?confirmationNumber=(\d+)?
I am new to RegEx and what I wrote above is based off the example at the following link:
http://www.sourcepole.ch/2011/1/4/extracting-text-from-a-page-and-using-it-somewhere-else-in-jmeter
I need this confirmation number for a dynamic page redirect for a Jmeter script I am writing. Any help would be greatly appreciated and if you require additional information to help answer the question let me know!
Many thanks in advance.
Try this: Location: [\S]+?confirmationNumber=(\d+)
Your issue is the use of special characters in the string without escaping them - e.g.: ? and /
Note my ? is not matching the question mark in front of confirmationNumber, but instead is making the [\S]+ non-greedy.
If you want to be explicit, your version should work if modified like this to escape the characters with special meaning:
Location: http:\/\/test.regtest.com\/cart\/confirmation\?confirmationNumber=(\d+)?
You don't need to match the entire line to get the confirmation number, instead you can just match the number like this:
(?<=confirmationNumber=)(\d+)
(?<=confirmationNumber=) is called a look behind, what the expression says is to match one more digits (\d+) and put them into a group, only if those digits are preceded by the following string confirmationNumber=.
Rege101 Demo
Regexp as following
confirmationNumber=([0-9]+)