how to find output lines between two patters(they are the same)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to parse some log file, which each line starts with time stamp, such as:
[11/16/18 16:40:04:097 EST]
If there isn't any error in the log, then every line will have the same starting pattern. However, if some error occurs, then the whole error stack will be printed with the time stamp as following:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
What I want to do is extra the whole error stack, for example, the input is:
[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:101 EST] 000000ae SystemErr R
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
The ideal out put should be:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
Tried the following and failed, if you can let me know what's wrong with my code, it would be great.
import re, sys
if len(sys.argv) > 1:
with open(sys.argv[1]) as f:
text = f.read()
else:
text = sys.stdin.read()
p_start = r'^[d{2}/.*'
p_end = r'^[d{2}/.*'
pattern = r'{p0}(?!.*{p0})(?:.*?{p1}|.*)'.format(p0=p_start, p1=p_end)
error_no_match = 'No Match'
matches = re.findall(pattern, text, flags=re.M|re.DOTALL)
if matches:
for match in matches:
print 'match:', match
print len(matches)
else:
print error_no_match
python regex
add a comment |
I'm trying to parse some log file, which each line starts with time stamp, such as:
[11/16/18 16:40:04:097 EST]
If there isn't any error in the log, then every line will have the same starting pattern. However, if some error occurs, then the whole error stack will be printed with the time stamp as following:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
What I want to do is extra the whole error stack, for example, the input is:
[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:101 EST] 000000ae SystemErr R
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
The ideal out put should be:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
Tried the following and failed, if you can let me know what's wrong with my code, it would be great.
import re, sys
if len(sys.argv) > 1:
with open(sys.argv[1]) as f:
text = f.read()
else:
text = sys.stdin.read()
p_start = r'^[d{2}/.*'
p_end = r'^[d{2}/.*'
pattern = r'{p0}(?!.*{p0})(?:.*?{p1}|.*)'.format(p0=p_start, p1=p_end)
error_no_match = 'No Match'
matches = re.findall(pattern, text, flags=re.M|re.DOTALL)
if matches:
for match in matches:
print 'match:', match
print len(matches)
else:
print error_no_match
python regex
You seem to needre.findall(r'(?m)^[d{2}/.*(?:n(?![d{2}/).*)+', text), see regex demo.
– Wiktor Stribiżew
Nov 26 '18 at 20:43
add a comment |
I'm trying to parse some log file, which each line starts with time stamp, such as:
[11/16/18 16:40:04:097 EST]
If there isn't any error in the log, then every line will have the same starting pattern. However, if some error occurs, then the whole error stack will be printed with the time stamp as following:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
What I want to do is extra the whole error stack, for example, the input is:
[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:101 EST] 000000ae SystemErr R
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
The ideal out put should be:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
Tried the following and failed, if you can let me know what's wrong with my code, it would be great.
import re, sys
if len(sys.argv) > 1:
with open(sys.argv[1]) as f:
text = f.read()
else:
text = sys.stdin.read()
p_start = r'^[d{2}/.*'
p_end = r'^[d{2}/.*'
pattern = r'{p0}(?!.*{p0})(?:.*?{p1}|.*)'.format(p0=p_start, p1=p_end)
error_no_match = 'No Match'
matches = re.findall(pattern, text, flags=re.M|re.DOTALL)
if matches:
for match in matches:
print 'match:', match
print len(matches)
else:
print error_no_match
python regex
I'm trying to parse some log file, which each line starts with time stamp, such as:
[11/16/18 16:40:04:097 EST]
If there isn't any error in the log, then every line will have the same starting pattern. However, if some error occurs, then the whole error stack will be printed with the time stamp as following:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
What I want to do is extra the whole error stack, for example, the input is:
[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:101 EST] 000000ae SystemErr R
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
The ideal out put should be:
[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName
Additional Data:
null
Current exception:
Message:
_ERR_BSAFE_FUNCTION
Stack trace:
[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.
at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)
Tried the following and failed, if you can let me know what's wrong with my code, it would be great.
import re, sys
if len(sys.argv) > 1:
with open(sys.argv[1]) as f:
text = f.read()
else:
text = sys.stdin.read()
p_start = r'^[d{2}/.*'
p_end = r'^[d{2}/.*'
pattern = r'{p0}(?!.*{p0})(?:.*?{p1}|.*)'.format(p0=p_start, p1=p_end)
error_no_match = 'No Match'
matches = re.findall(pattern, text, flags=re.M|re.DOTALL)
if matches:
for match in matches:
print 'match:', match
print len(matches)
else:
print error_no_match
python regex
python regex
asked Nov 26 '18 at 20:21
Ian ZhangIan Zhang
919
919
You seem to needre.findall(r'(?m)^[d{2}/.*(?:n(?![d{2}/).*)+', text), see regex demo.
– Wiktor Stribiżew
Nov 26 '18 at 20:43
add a comment |
You seem to needre.findall(r'(?m)^[d{2}/.*(?:n(?![d{2}/).*)+', text), see regex demo.
– Wiktor Stribiżew
Nov 26 '18 at 20:43
You seem to need
re.findall(r'(?m)^[d{2}/.*(?:n(?![d{2}/).*)+', text), see regex demo.– Wiktor Stribiżew
Nov 26 '18 at 20:43
You seem to need
re.findall(r'(?m)^[d{2}/.*(?:n(?![d{2}/).*)+', text), see regex demo.– Wiktor Stribiżew
Nov 26 '18 at 20:43
add a comment |
1 Answer
1
active
oldest
votes
As you read the whole file into a variable text, you may use
matches = re.findall(r'^[d{2}/.*(?:n(?![d{2}/).*)+', text, re.M)
See regex demo. Note that in case your text contains CRLF endings, you need to replace n with r?n (where CR is optional).
Details
re.Mmodifier makes^match at the start of a line
^- start of a line
[- a[char
d{2}/- 2 digits and a/char
.*- the rest of the line
(?:n(?![d{2}/).*)+- one or more repetitions of
n(?![d{2}/)- an LF symbol (user?nif there can be CRLF endings) that is not followed with[and two digits and/
.*- the rest of the line.
Python demo:
import re
rx = r"^[d{2}/.*(?:n(?![d{2}/).*)+"
text = "[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0 n[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:n[11/16/18 16:40:04:101 EST] 000000ae SystemErr R n[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)"
matches = re.findall(rx, text, re.M)
print(matches)
Output:
[
'[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:',
'[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)'
]
1
Thanks for your help, Wiktor. I had hard time to figure out .*(?:n(?![d{2}/).*)+' by myself... do you have any idea, where did I got wrong...
– Ian Zhang
Nov 26 '18 at 20:55
@IanZhang It is because you tried to match something in between while here it is easier to get all consecutive lines that do not start with a certain pattern, and+quantifier at the end is very important.
– Wiktor Stribiżew
Nov 26 '18 at 20:56
In.*(?:n(?![d{2}/).*)+', the[is not escaped. You do not match the starting pattern either, although that is of minor importance.
– Wiktor Stribiżew
Nov 26 '18 at 20:59
thanks, also, for +, it's aginst .*(?:n(?![d{2}/).*) or the the whole patter ^[d{2}/.*(?:n(?![d{2}/).*)? In addition, got confused by ?: can you please explain this a bit?
– Ian Zhang
Nov 26 '18 at 21:04
@IanZhang(?:...)is a non-capturing group, only meant to group a number of patterns into a sequence of patterns that you may quantify or use several alternatives. Last+in my pattern makes sure the regex only matches multiline entries.
– Wiktor Stribiżew
Nov 26 '18 at 21:17
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488453%2fhow-to-find-output-lines-between-two-pattersthey-are-the-same%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As you read the whole file into a variable text, you may use
matches = re.findall(r'^[d{2}/.*(?:n(?![d{2}/).*)+', text, re.M)
See regex demo. Note that in case your text contains CRLF endings, you need to replace n with r?n (where CR is optional).
Details
re.Mmodifier makes^match at the start of a line
^- start of a line
[- a[char
d{2}/- 2 digits and a/char
.*- the rest of the line
(?:n(?![d{2}/).*)+- one or more repetitions of
n(?![d{2}/)- an LF symbol (user?nif there can be CRLF endings) that is not followed with[and two digits and/
.*- the rest of the line.
Python demo:
import re
rx = r"^[d{2}/.*(?:n(?![d{2}/).*)+"
text = "[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0 n[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:n[11/16/18 16:40:04:101 EST] 000000ae SystemErr R n[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)"
matches = re.findall(rx, text, re.M)
print(matches)
Output:
[
'[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:',
'[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)'
]
1
Thanks for your help, Wiktor. I had hard time to figure out .*(?:n(?![d{2}/).*)+' by myself... do you have any idea, where did I got wrong...
– Ian Zhang
Nov 26 '18 at 20:55
@IanZhang It is because you tried to match something in between while here it is easier to get all consecutive lines that do not start with a certain pattern, and+quantifier at the end is very important.
– Wiktor Stribiżew
Nov 26 '18 at 20:56
In.*(?:n(?![d{2}/).*)+', the[is not escaped. You do not match the starting pattern either, although that is of minor importance.
– Wiktor Stribiżew
Nov 26 '18 at 20:59
thanks, also, for +, it's aginst .*(?:n(?![d{2}/).*) or the the whole patter ^[d{2}/.*(?:n(?![d{2}/).*)? In addition, got confused by ?: can you please explain this a bit?
– Ian Zhang
Nov 26 '18 at 21:04
@IanZhang(?:...)is a non-capturing group, only meant to group a number of patterns into a sequence of patterns that you may quantify or use several alternatives. Last+in my pattern makes sure the regex only matches multiline entries.
– Wiktor Stribiżew
Nov 26 '18 at 21:17
|
show 1 more comment
As you read the whole file into a variable text, you may use
matches = re.findall(r'^[d{2}/.*(?:n(?![d{2}/).*)+', text, re.M)
See regex demo. Note that in case your text contains CRLF endings, you need to replace n with r?n (where CR is optional).
Details
re.Mmodifier makes^match at the start of a line
^- start of a line
[- a[char
d{2}/- 2 digits and a/char
.*- the rest of the line
(?:n(?![d{2}/).*)+- one or more repetitions of
n(?![d{2}/)- an LF symbol (user?nif there can be CRLF endings) that is not followed with[and two digits and/
.*- the rest of the line.
Python demo:
import re
rx = r"^[d{2}/.*(?:n(?![d{2}/).*)+"
text = "[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0 n[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:n[11/16/18 16:40:04:101 EST] 000000ae SystemErr R n[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)"
matches = re.findall(rx, text, re.M)
print(matches)
Output:
[
'[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:',
'[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)'
]
1
Thanks for your help, Wiktor. I had hard time to figure out .*(?:n(?![d{2}/).*)+' by myself... do you have any idea, where did I got wrong...
– Ian Zhang
Nov 26 '18 at 20:55
@IanZhang It is because you tried to match something in between while here it is easier to get all consecutive lines that do not start with a certain pattern, and+quantifier at the end is very important.
– Wiktor Stribiżew
Nov 26 '18 at 20:56
In.*(?:n(?![d{2}/).*)+', the[is not escaped. You do not match the starting pattern either, although that is of minor importance.
– Wiktor Stribiżew
Nov 26 '18 at 20:59
thanks, also, for +, it's aginst .*(?:n(?![d{2}/).*) or the the whole patter ^[d{2}/.*(?:n(?![d{2}/).*)? In addition, got confused by ?: can you please explain this a bit?
– Ian Zhang
Nov 26 '18 at 21:04
@IanZhang(?:...)is a non-capturing group, only meant to group a number of patterns into a sequence of patterns that you may quantify or use several alternatives. Last+in my pattern makes sure the regex only matches multiline entries.
– Wiktor Stribiżew
Nov 26 '18 at 21:17
|
show 1 more comment
As you read the whole file into a variable text, you may use
matches = re.findall(r'^[d{2}/.*(?:n(?![d{2}/).*)+', text, re.M)
See regex demo. Note that in case your text contains CRLF endings, you need to replace n with r?n (where CR is optional).
Details
re.Mmodifier makes^match at the start of a line
^- start of a line
[- a[char
d{2}/- 2 digits and a/char
.*- the rest of the line
(?:n(?![d{2}/).*)+- one or more repetitions of
n(?![d{2}/)- an LF symbol (user?nif there can be CRLF endings) that is not followed with[and two digits and/
.*- the rest of the line.
Python demo:
import re
rx = r"^[d{2}/.*(?:n(?![d{2}/).*)+"
text = "[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0 n[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:n[11/16/18 16:40:04:101 EST] 000000ae SystemErr R n[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)"
matches = re.findall(rx, text, re.M)
print(matches)
Output:
[
'[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:',
'[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)'
]
As you read the whole file into a variable text, you may use
matches = re.findall(r'^[d{2}/.*(?:n(?![d{2}/).*)+', text, re.M)
See regex demo. Note that in case your text contains CRLF endings, you need to replace n with r?n (where CR is optional).
Details
re.Mmodifier makes^match at the start of a line
^- start of a line
[- a[char
d{2}/- 2 digits and a/char
.*- the rest of the line
(?:n(?![d{2}/).*)+- one or more repetitions of
n(?![d{2}/)- an LF symbol (user?nif there can be CRLF endings) that is not followed with[and two digits and/
.*- the rest of the line.
Python demo:
import re
rx = r"^[d{2}/.*(?:n(?![d{2}/).*)+"
text = "[11/16/18 16:40:04:098 EST] 000000ae CommandLogger 2 PerfLog <entry operation="Command : com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl" parameters="@releaseID=9.0 n[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:n[11/16/18 16:40:04:101 EST] 000000ae SystemErr R n[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)"
matches = re.findall(rx, text, re.M)
print(matches)
Output:
[
'[11/16/18 16:40:04:100 EST] 000000ae CommerceSrvr E MessagingViewCommandImpl nonHttpForwardDocument(String,String) CMN8014E: The URL constructed during composition using ViewName nAdditional Data: n nullnCurrent exception:nMessage:n_ERR_BSAFE_FUNCTIONnStack trace:',
'[11/16/18 16:40:04:102 EST] 000000ae SystemErr R com.ibm.commerce.exception.ECSystemException: The URL constructed during composition using ViewName http://localhost:80/webapp/wcs/stores/IBM.WC.Compose/webservices/OAGIS/9.0/BODs/AcknowledgePaymentInstruction.jsp/******** is invalid {1}.n at com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl.nonHttpForwardDocument(MessagingViewCommandImpl.java:581)'
]
edited Nov 26 '18 at 20:55
answered Nov 26 '18 at 20:46
Wiktor StribiżewWiktor Stribiżew
330k16149229
330k16149229
1
Thanks for your help, Wiktor. I had hard time to figure out .*(?:n(?![d{2}/).*)+' by myself... do you have any idea, where did I got wrong...
– Ian Zhang
Nov 26 '18 at 20:55
@IanZhang It is because you tried to match something in between while here it is easier to get all consecutive lines that do not start with a certain pattern, and+quantifier at the end is very important.
– Wiktor Stribiżew
Nov 26 '18 at 20:56
In.*(?:n(?![d{2}/).*)+', the[is not escaped. You do not match the starting pattern either, although that is of minor importance.
– Wiktor Stribiżew
Nov 26 '18 at 20:59
thanks, also, for +, it's aginst .*(?:n(?![d{2}/).*) or the the whole patter ^[d{2}/.*(?:n(?![d{2}/).*)? In addition, got confused by ?: can you please explain this a bit?
– Ian Zhang
Nov 26 '18 at 21:04
@IanZhang(?:...)is a non-capturing group, only meant to group a number of patterns into a sequence of patterns that you may quantify or use several alternatives. Last+in my pattern makes sure the regex only matches multiline entries.
– Wiktor Stribiżew
Nov 26 '18 at 21:17
|
show 1 more comment
1
Thanks for your help, Wiktor. I had hard time to figure out .*(?:n(?![d{2}/).*)+' by myself... do you have any idea, where did I got wrong...
– Ian Zhang
Nov 26 '18 at 20:55
@IanZhang It is because you tried to match something in between while here it is easier to get all consecutive lines that do not start with a certain pattern, and+quantifier at the end is very important.
– Wiktor Stribiżew
Nov 26 '18 at 20:56
In.*(?:n(?![d{2}/).*)+', the[is not escaped. You do not match the starting pattern either, although that is of minor importance.
– Wiktor Stribiżew
Nov 26 '18 at 20:59
thanks, also, for +, it's aginst .*(?:n(?![d{2}/).*) or the the whole patter ^[d{2}/.*(?:n(?![d{2}/).*)? In addition, got confused by ?: can you please explain this a bit?
– Ian Zhang
Nov 26 '18 at 21:04
@IanZhang(?:...)is a non-capturing group, only meant to group a number of patterns into a sequence of patterns that you may quantify or use several alternatives. Last+in my pattern makes sure the regex only matches multiline entries.
– Wiktor Stribiżew
Nov 26 '18 at 21:17
1
1
Thanks for your help, Wiktor. I had hard time to figure out .*(?:n(?![d{2}/).*)+' by myself... do you have any idea, where did I got wrong...
– Ian Zhang
Nov 26 '18 at 20:55
Thanks for your help, Wiktor. I had hard time to figure out .*(?:n(?![d{2}/).*)+' by myself... do you have any idea, where did I got wrong...
– Ian Zhang
Nov 26 '18 at 20:55
@IanZhang It is because you tried to match something in between while here it is easier to get all consecutive lines that do not start with a certain pattern, and
+ quantifier at the end is very important.– Wiktor Stribiżew
Nov 26 '18 at 20:56
@IanZhang It is because you tried to match something in between while here it is easier to get all consecutive lines that do not start with a certain pattern, and
+ quantifier at the end is very important.– Wiktor Stribiżew
Nov 26 '18 at 20:56
In
.*(?:n(?![d{2}/).*)+', the [ is not escaped. You do not match the starting pattern either, although that is of minor importance.– Wiktor Stribiżew
Nov 26 '18 at 20:59
In
.*(?:n(?![d{2}/).*)+', the [ is not escaped. You do not match the starting pattern either, although that is of minor importance.– Wiktor Stribiżew
Nov 26 '18 at 20:59
thanks, also, for +, it's aginst .*(?:n(?![d{2}/).*) or the the whole patter ^[d{2}/.*(?:n(?![d{2}/).*)? In addition, got confused by ?: can you please explain this a bit?
– Ian Zhang
Nov 26 '18 at 21:04
thanks, also, for +, it's aginst .*(?:n(?![d{2}/).*) or the the whole patter ^[d{2}/.*(?:n(?![d{2}/).*)? In addition, got confused by ?: can you please explain this a bit?
– Ian Zhang
Nov 26 '18 at 21:04
@IanZhang
(?:...) is a non-capturing group, only meant to group a number of patterns into a sequence of patterns that you may quantify or use several alternatives. Last + in my pattern makes sure the regex only matches multiline entries.– Wiktor Stribiżew
Nov 26 '18 at 21:17
@IanZhang
(?:...) is a non-capturing group, only meant to group a number of patterns into a sequence of patterns that you may quantify or use several alternatives. Last + in my pattern makes sure the regex only matches multiline entries.– Wiktor Stribiżew
Nov 26 '18 at 21:17
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53488453%2fhow-to-find-output-lines-between-two-pattersthey-are-the-same%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You seem to need
re.findall(r'(?m)^[d{2}/.*(?:n(?![d{2}/).*)+', text), see regex demo.– Wiktor Stribiżew
Nov 26 '18 at 20:43