Extract numbers from line not starting with comment symbol using regex
I'm try to replace all numbers not in the comment section. Here is a sample of the file to fix:
/* 2018-01-01 06:00:55 : realtime(0.002) --status(10)-- ++numretLines(0)++ --IP(192.168.1.5) PORT(22)-- queryNo(2) comment[TO: Too much time] TYPE[QUERY 4.2] */
select count(*) from table where id1 = 41111 and id2 = 221144
GO
Basically, I would like to replace numbers in strings not beginning with "/*"
.
I came up with the following regex: /^(?!/*)(?:.+K(d+?))/gmU
But I only manage to extract the first number of each line not starting with "/*"
. How could I extend this to get all the numbers of those rows?
Thanks!
regex negative-lookahead
add a comment |
I'm try to replace all numbers not in the comment section. Here is a sample of the file to fix:
/* 2018-01-01 06:00:55 : realtime(0.002) --status(10)-- ++numretLines(0)++ --IP(192.168.1.5) PORT(22)-- queryNo(2) comment[TO: Too much time] TYPE[QUERY 4.2] */
select count(*) from table where id1 = 41111 and id2 = 221144
GO
Basically, I would like to replace numbers in strings not beginning with "/*"
.
I came up with the following regex: /^(?!/*)(?:.+K(d+?))/gmU
But I only manage to extract the first number of each line not starting with "/*"
. How could I extend this to get all the numbers of those rows?
Thanks!
regex negative-lookahead
See regex101.com/r/iRNQOG/1
– Wiktor Stribiżew
Nov 28 '18 at 17:03
1
What technology are you using for your regex?
– Alfredo A.
Nov 28 '18 at 17:52
Looks like a good answer! Thanks! Would you like to put it as an answer so that I can mark it as the correct answer?
– Maxime Dumas
Nov 28 '18 at 17:55
@AlfredoA. I'm using Perl
– Maxime Dumas
Nov 28 '18 at 18:12
add a comment |
I'm try to replace all numbers not in the comment section. Here is a sample of the file to fix:
/* 2018-01-01 06:00:55 : realtime(0.002) --status(10)-- ++numretLines(0)++ --IP(192.168.1.5) PORT(22)-- queryNo(2) comment[TO: Too much time] TYPE[QUERY 4.2] */
select count(*) from table where id1 = 41111 and id2 = 221144
GO
Basically, I would like to replace numbers in strings not beginning with "/*"
.
I came up with the following regex: /^(?!/*)(?:.+K(d+?))/gmU
But I only manage to extract the first number of each line not starting with "/*"
. How could I extend this to get all the numbers of those rows?
Thanks!
regex negative-lookahead
I'm try to replace all numbers not in the comment section. Here is a sample of the file to fix:
/* 2018-01-01 06:00:55 : realtime(0.002) --status(10)-- ++numretLines(0)++ --IP(192.168.1.5) PORT(22)-- queryNo(2) comment[TO: Too much time] TYPE[QUERY 4.2] */
select count(*) from table where id1 = 41111 and id2 = 221144
GO
Basically, I would like to replace numbers in strings not beginning with "/*"
.
I came up with the following regex: /^(?!/*)(?:.+K(d+?))/gmU
But I only manage to extract the first number of each line not starting with "/*"
. How could I extend this to get all the numbers of those rows?
Thanks!
regex negative-lookahead
regex negative-lookahead
edited Nov 28 '18 at 17:02
Wiktor Stribiżew
326k16147226
326k16147226
asked Nov 28 '18 at 17:01
Maxime DumasMaxime Dumas
112
112
See regex101.com/r/iRNQOG/1
– Wiktor Stribiżew
Nov 28 '18 at 17:03
1
What technology are you using for your regex?
– Alfredo A.
Nov 28 '18 at 17:52
Looks like a good answer! Thanks! Would you like to put it as an answer so that I can mark it as the correct answer?
– Maxime Dumas
Nov 28 '18 at 17:55
@AlfredoA. I'm using Perl
– Maxime Dumas
Nov 28 '18 at 18:12
add a comment |
See regex101.com/r/iRNQOG/1
– Wiktor Stribiżew
Nov 28 '18 at 17:03
1
What technology are you using for your regex?
– Alfredo A.
Nov 28 '18 at 17:52
Looks like a good answer! Thanks! Would you like to put it as an answer so that I can mark it as the correct answer?
– Maxime Dumas
Nov 28 '18 at 17:55
@AlfredoA. I'm using Perl
– Maxime Dumas
Nov 28 '18 at 18:12
See regex101.com/r/iRNQOG/1
– Wiktor Stribiżew
Nov 28 '18 at 17:03
See regex101.com/r/iRNQOG/1
– Wiktor Stribiżew
Nov 28 '18 at 17:03
1
1
What technology are you using for your regex?
– Alfredo A.
Nov 28 '18 at 17:52
What technology are you using for your regex?
– Alfredo A.
Nov 28 '18 at 17:52
Looks like a good answer! Thanks! Would you like to put it as an answer so that I can mark it as the correct answer?
– Maxime Dumas
Nov 28 '18 at 17:55
Looks like a good answer! Thanks! Would you like to put it as an answer so that I can mark it as the correct answer?
– Maxime Dumas
Nov 28 '18 at 17:55
@AlfredoA. I'm using Perl
– Maxime Dumas
Nov 28 '18 at 18:12
@AlfredoA. I'm using Perl
– Maxime Dumas
Nov 28 '18 at 18:12
add a comment |
2 Answers
2
active
oldest
votes
Assuming your regex engine (which you haven't told) supports look behind
and look ahead
, you can use this regex:
(?<!^/*.*)(?:(?<=s)d+(?=s))+
The regex starts by using a negative look behind
, looking for the start of line
, followed by a slash
and a star
.
Then it creates a new negative look behind
for a White Space
, then any number of digits
, followed by a negative look ahead
for a White Space
. This Group is repeated any number of times
.
You need to set the global
and 'multiline'
flag.
The regex skips numbers not surrounded by White Space (for instance 'id1'
)
I tried your regex in regex101.com, and it was complaining about * "A quantifier inside a lookbehind makes it non-fixed width". Appears to work, depending on the "flavor" I select. Is it something that is not supported by all languages?
– Maxime Dumas
Nov 29 '18 at 15:35
Exactly, it's not supported by all flavors of regex. (You didn't write what flavor you use)
– Poul Bak
Nov 29 '18 at 17:46
add a comment |
Based on Wiktor Stribiżew comment, I used /*.*?*/(*SKIP)(*F)|-?bd+(.d+)?
to extract the numbers, including decimals and negative values.
add a 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%2f53524575%2fextract-numbers-from-line-not-starting-with-comment-symbol-using-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming your regex engine (which you haven't told) supports look behind
and look ahead
, you can use this regex:
(?<!^/*.*)(?:(?<=s)d+(?=s))+
The regex starts by using a negative look behind
, looking for the start of line
, followed by a slash
and a star
.
Then it creates a new negative look behind
for a White Space
, then any number of digits
, followed by a negative look ahead
for a White Space
. This Group is repeated any number of times
.
You need to set the global
and 'multiline'
flag.
The regex skips numbers not surrounded by White Space (for instance 'id1'
)
I tried your regex in regex101.com, and it was complaining about * "A quantifier inside a lookbehind makes it non-fixed width". Appears to work, depending on the "flavor" I select. Is it something that is not supported by all languages?
– Maxime Dumas
Nov 29 '18 at 15:35
Exactly, it's not supported by all flavors of regex. (You didn't write what flavor you use)
– Poul Bak
Nov 29 '18 at 17:46
add a comment |
Assuming your regex engine (which you haven't told) supports look behind
and look ahead
, you can use this regex:
(?<!^/*.*)(?:(?<=s)d+(?=s))+
The regex starts by using a negative look behind
, looking for the start of line
, followed by a slash
and a star
.
Then it creates a new negative look behind
for a White Space
, then any number of digits
, followed by a negative look ahead
for a White Space
. This Group is repeated any number of times
.
You need to set the global
and 'multiline'
flag.
The regex skips numbers not surrounded by White Space (for instance 'id1'
)
I tried your regex in regex101.com, and it was complaining about * "A quantifier inside a lookbehind makes it non-fixed width". Appears to work, depending on the "flavor" I select. Is it something that is not supported by all languages?
– Maxime Dumas
Nov 29 '18 at 15:35
Exactly, it's not supported by all flavors of regex. (You didn't write what flavor you use)
– Poul Bak
Nov 29 '18 at 17:46
add a comment |
Assuming your regex engine (which you haven't told) supports look behind
and look ahead
, you can use this regex:
(?<!^/*.*)(?:(?<=s)d+(?=s))+
The regex starts by using a negative look behind
, looking for the start of line
, followed by a slash
and a star
.
Then it creates a new negative look behind
for a White Space
, then any number of digits
, followed by a negative look ahead
for a White Space
. This Group is repeated any number of times
.
You need to set the global
and 'multiline'
flag.
The regex skips numbers not surrounded by White Space (for instance 'id1'
)
Assuming your regex engine (which you haven't told) supports look behind
and look ahead
, you can use this regex:
(?<!^/*.*)(?:(?<=s)d+(?=s))+
The regex starts by using a negative look behind
, looking for the start of line
, followed by a slash
and a star
.
Then it creates a new negative look behind
for a White Space
, then any number of digits
, followed by a negative look ahead
for a White Space
. This Group is repeated any number of times
.
You need to set the global
and 'multiline'
flag.
The regex skips numbers not surrounded by White Space (for instance 'id1'
)
edited Nov 28 '18 at 18:05
answered Nov 28 '18 at 17:59
Poul BakPoul Bak
5,47831233
5,47831233
I tried your regex in regex101.com, and it was complaining about * "A quantifier inside a lookbehind makes it non-fixed width". Appears to work, depending on the "flavor" I select. Is it something that is not supported by all languages?
– Maxime Dumas
Nov 29 '18 at 15:35
Exactly, it's not supported by all flavors of regex. (You didn't write what flavor you use)
– Poul Bak
Nov 29 '18 at 17:46
add a comment |
I tried your regex in regex101.com, and it was complaining about * "A quantifier inside a lookbehind makes it non-fixed width". Appears to work, depending on the "flavor" I select. Is it something that is not supported by all languages?
– Maxime Dumas
Nov 29 '18 at 15:35
Exactly, it's not supported by all flavors of regex. (You didn't write what flavor you use)
– Poul Bak
Nov 29 '18 at 17:46
I tried your regex in regex101.com, and it was complaining about * "A quantifier inside a lookbehind makes it non-fixed width". Appears to work, depending on the "flavor" I select. Is it something that is not supported by all languages?
– Maxime Dumas
Nov 29 '18 at 15:35
I tried your regex in regex101.com, and it was complaining about * "A quantifier inside a lookbehind makes it non-fixed width". Appears to work, depending on the "flavor" I select. Is it something that is not supported by all languages?
– Maxime Dumas
Nov 29 '18 at 15:35
Exactly, it's not supported by all flavors of regex. (You didn't write what flavor you use)
– Poul Bak
Nov 29 '18 at 17:46
Exactly, it's not supported by all flavors of regex. (You didn't write what flavor you use)
– Poul Bak
Nov 29 '18 at 17:46
add a comment |
Based on Wiktor Stribiżew comment, I used /*.*?*/(*SKIP)(*F)|-?bd+(.d+)?
to extract the numbers, including decimals and negative values.
add a comment |
Based on Wiktor Stribiżew comment, I used /*.*?*/(*SKIP)(*F)|-?bd+(.d+)?
to extract the numbers, including decimals and negative values.
add a comment |
Based on Wiktor Stribiżew comment, I used /*.*?*/(*SKIP)(*F)|-?bd+(.d+)?
to extract the numbers, including decimals and negative values.
Based on Wiktor Stribiżew comment, I used /*.*?*/(*SKIP)(*F)|-?bd+(.d+)?
to extract the numbers, including decimals and negative values.
answered Nov 29 '18 at 15:33
Maxime DumasMaxime Dumas
112
112
add a comment |
add a 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%2f53524575%2fextract-numbers-from-line-not-starting-with-comment-symbol-using-regex%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
See regex101.com/r/iRNQOG/1
– Wiktor Stribiżew
Nov 28 '18 at 17:03
1
What technology are you using for your regex?
– Alfredo A.
Nov 28 '18 at 17:52
Looks like a good answer! Thanks! Would you like to put it as an answer so that I can mark it as the correct answer?
– Maxime Dumas
Nov 28 '18 at 17:55
@AlfredoA. I'm using Perl
– Maxime Dumas
Nov 28 '18 at 18:12