How can I get the regex equivalent if the expression are in separate lines?
I have the following string:
Datum Kundnummer Sida
2018-10-12 196979 1 /2
The two strings above are in separate lines
What is the regex equivalent so that I get the following output?
Datum 2018-10-12
Kundnummer 196979
The above two are separate outputs
I want the output such that it works for all dates in datum and all numbers in Kundnummer.
I tried Datums([12]d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]d|3[01]))
and Kundnummers(d+)
.
python regex
add a comment |
I have the following string:
Datum Kundnummer Sida
2018-10-12 196979 1 /2
The two strings above are in separate lines
What is the regex equivalent so that I get the following output?
Datum 2018-10-12
Kundnummer 196979
The above two are separate outputs
I want the output such that it works for all dates in datum and all numbers in Kundnummer.
I tried Datums([12]d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]d|3[01]))
and Kundnummers(d+)
.
python regex
If all your data contains is those two lines and the content of the first line is constant, don't bother matching the first line at all. Match something that conforms to the format of the second line and extracts the relevant parts, like([12]d{3}-0[1-9]|1[0-2]-0[1-9]|[12]d|3[01])s(d+)
and replace byDatum 1nKundnummer 2
– Aaron
2 days ago
Oh and the big problem with the regex you tried is that while it would perfectly match the format of your expected output, what you need it to match is the format of your input.
– Aaron
2 days ago
How can I replace it..Can you just explain that!
– RAM SHANKER G
2 days ago
Sorry, I don't do much python. Look into the "re" module's documentation.
– Aaron
2 days ago
Why would you use regex for this? It seems like overkill.
– trincot
2 days ago
add a comment |
I have the following string:
Datum Kundnummer Sida
2018-10-12 196979 1 /2
The two strings above are in separate lines
What is the regex equivalent so that I get the following output?
Datum 2018-10-12
Kundnummer 196979
The above two are separate outputs
I want the output such that it works for all dates in datum and all numbers in Kundnummer.
I tried Datums([12]d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]d|3[01]))
and Kundnummers(d+)
.
python regex
I have the following string:
Datum Kundnummer Sida
2018-10-12 196979 1 /2
The two strings above are in separate lines
What is the regex equivalent so that I get the following output?
Datum 2018-10-12
Kundnummer 196979
The above two are separate outputs
I want the output such that it works for all dates in datum and all numbers in Kundnummer.
I tried Datums([12]d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]d|3[01]))
and Kundnummers(d+)
.
python regex
python regex
edited 2 days ago
Wiktor Stribiżew
307k16126202
307k16126202
asked 2 days ago
RAM SHANKER G
167
167
If all your data contains is those two lines and the content of the first line is constant, don't bother matching the first line at all. Match something that conforms to the format of the second line and extracts the relevant parts, like([12]d{3}-0[1-9]|1[0-2]-0[1-9]|[12]d|3[01])s(d+)
and replace byDatum 1nKundnummer 2
– Aaron
2 days ago
Oh and the big problem with the regex you tried is that while it would perfectly match the format of your expected output, what you need it to match is the format of your input.
– Aaron
2 days ago
How can I replace it..Can you just explain that!
– RAM SHANKER G
2 days ago
Sorry, I don't do much python. Look into the "re" module's documentation.
– Aaron
2 days ago
Why would you use regex for this? It seems like overkill.
– trincot
2 days ago
add a comment |
If all your data contains is those two lines and the content of the first line is constant, don't bother matching the first line at all. Match something that conforms to the format of the second line and extracts the relevant parts, like([12]d{3}-0[1-9]|1[0-2]-0[1-9]|[12]d|3[01])s(d+)
and replace byDatum 1nKundnummer 2
– Aaron
2 days ago
Oh and the big problem with the regex you tried is that while it would perfectly match the format of your expected output, what you need it to match is the format of your input.
– Aaron
2 days ago
How can I replace it..Can you just explain that!
– RAM SHANKER G
2 days ago
Sorry, I don't do much python. Look into the "re" module's documentation.
– Aaron
2 days ago
Why would you use regex for this? It seems like overkill.
– trincot
2 days ago
If all your data contains is those two lines and the content of the first line is constant, don't bother matching the first line at all. Match something that conforms to the format of the second line and extracts the relevant parts, like
([12]d{3}-0[1-9]|1[0-2]-0[1-9]|[12]d|3[01])s(d+)
and replace by Datum 1nKundnummer 2
– Aaron
2 days ago
If all your data contains is those two lines and the content of the first line is constant, don't bother matching the first line at all. Match something that conforms to the format of the second line and extracts the relevant parts, like
([12]d{3}-0[1-9]|1[0-2]-0[1-9]|[12]d|3[01])s(d+)
and replace by Datum 1nKundnummer 2
– Aaron
2 days ago
Oh and the big problem with the regex you tried is that while it would perfectly match the format of your expected output, what you need it to match is the format of your input.
– Aaron
2 days ago
Oh and the big problem with the regex you tried is that while it would perfectly match the format of your expected output, what you need it to match is the format of your input.
– Aaron
2 days ago
How can I replace it..Can you just explain that!
– RAM SHANKER G
2 days ago
How can I replace it..Can you just explain that!
– RAM SHANKER G
2 days ago
Sorry, I don't do much python. Look into the "re" module's documentation.
– Aaron
2 days ago
Sorry, I don't do much python. Look into the "re" module's documentation.
– Aaron
2 days ago
Why would you use regex for this? It seems like overkill.
– trincot
2 days ago
Why would you use regex for this? It seems like overkill.
– trincot
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
I assume the data in the first line is always the same, so I'd ignore it.
To get the "Datum" and "Kundennummer" entries from the second line, use (?P<datum>d{4}-d{1,2}-d{1,2})s(?P<kundennummer>d+?).*
.
As @PyHunterMan explained, you could also just split the string at the whitespaces. That would probably be the easier way.
add a comment |
Why would you wage on time figuring this out when you can do it in a simple lines of code without regex.
_input = "Datum Kundnummer Sidan2018-10-12 196979 1 /2"
lines = _input.split('n')
old_line_one, old_line_two = lines[0].split(), lines[1].split()
new_line_one = f'{old_line_one[0]} {old_line_two[0]}'
new_line_two = f'{old_line_one[1]} {old_line_two[1]}'
print(f'{new_line_one}n{new_line_two}')
I agree. regex is overkill for this.
– trincot
2 days ago
add a comment |
I suggest something like this:
reobj = re.compile(r"(?P<datum>d{4}-d{1,2}-d{1,2})s+(?P<kundnummer>d+)")
match = reobj.search(input)
if match:
result1 = match.group("datum")
result2 = match.group("kundnummer")
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%2f53945344%2fhow-can-i-get-the-regex-equivalent-if-the-expression-are-in-separate-lines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I assume the data in the first line is always the same, so I'd ignore it.
To get the "Datum" and "Kundennummer" entries from the second line, use (?P<datum>d{4}-d{1,2}-d{1,2})s(?P<kundennummer>d+?).*
.
As @PyHunterMan explained, you could also just split the string at the whitespaces. That would probably be the easier way.
add a comment |
I assume the data in the first line is always the same, so I'd ignore it.
To get the "Datum" and "Kundennummer" entries from the second line, use (?P<datum>d{4}-d{1,2}-d{1,2})s(?P<kundennummer>d+?).*
.
As @PyHunterMan explained, you could also just split the string at the whitespaces. That would probably be the easier way.
add a comment |
I assume the data in the first line is always the same, so I'd ignore it.
To get the "Datum" and "Kundennummer" entries from the second line, use (?P<datum>d{4}-d{1,2}-d{1,2})s(?P<kundennummer>d+?).*
.
As @PyHunterMan explained, you could also just split the string at the whitespaces. That would probably be the easier way.
I assume the data in the first line is always the same, so I'd ignore it.
To get the "Datum" and "Kundennummer" entries from the second line, use (?P<datum>d{4}-d{1,2}-d{1,2})s(?P<kundennummer>d+?).*
.
As @PyHunterMan explained, you could also just split the string at the whitespaces. That would probably be the easier way.
answered 2 days ago
David3103
556
556
add a comment |
add a comment |
Why would you wage on time figuring this out when you can do it in a simple lines of code without regex.
_input = "Datum Kundnummer Sidan2018-10-12 196979 1 /2"
lines = _input.split('n')
old_line_one, old_line_two = lines[0].split(), lines[1].split()
new_line_one = f'{old_line_one[0]} {old_line_two[0]}'
new_line_two = f'{old_line_one[1]} {old_line_two[1]}'
print(f'{new_line_one}n{new_line_two}')
I agree. regex is overkill for this.
– trincot
2 days ago
add a comment |
Why would you wage on time figuring this out when you can do it in a simple lines of code without regex.
_input = "Datum Kundnummer Sidan2018-10-12 196979 1 /2"
lines = _input.split('n')
old_line_one, old_line_two = lines[0].split(), lines[1].split()
new_line_one = f'{old_line_one[0]} {old_line_two[0]}'
new_line_two = f'{old_line_one[1]} {old_line_two[1]}'
print(f'{new_line_one}n{new_line_two}')
I agree. regex is overkill for this.
– trincot
2 days ago
add a comment |
Why would you wage on time figuring this out when you can do it in a simple lines of code without regex.
_input = "Datum Kundnummer Sidan2018-10-12 196979 1 /2"
lines = _input.split('n')
old_line_one, old_line_two = lines[0].split(), lines[1].split()
new_line_one = f'{old_line_one[0]} {old_line_two[0]}'
new_line_two = f'{old_line_one[1]} {old_line_two[1]}'
print(f'{new_line_one}n{new_line_two}')
Why would you wage on time figuring this out when you can do it in a simple lines of code without regex.
_input = "Datum Kundnummer Sidan2018-10-12 196979 1 /2"
lines = _input.split('n')
old_line_one, old_line_two = lines[0].split(), lines[1].split()
new_line_one = f'{old_line_one[0]} {old_line_two[0]}'
new_line_two = f'{old_line_one[1]} {old_line_two[1]}'
print(f'{new_line_one}n{new_line_two}')
answered 2 days ago
PyHunterMan
184
184
I agree. regex is overkill for this.
– trincot
2 days ago
add a comment |
I agree. regex is overkill for this.
– trincot
2 days ago
I agree. regex is overkill for this.
– trincot
2 days ago
I agree. regex is overkill for this.
– trincot
2 days ago
add a comment |
I suggest something like this:
reobj = re.compile(r"(?P<datum>d{4}-d{1,2}-d{1,2})s+(?P<kundnummer>d+)")
match = reobj.search(input)
if match:
result1 = match.group("datum")
result2 = match.group("kundnummer")
add a comment |
I suggest something like this:
reobj = re.compile(r"(?P<datum>d{4}-d{1,2}-d{1,2})s+(?P<kundnummer>d+)")
match = reobj.search(input)
if match:
result1 = match.group("datum")
result2 = match.group("kundnummer")
add a comment |
I suggest something like this:
reobj = re.compile(r"(?P<datum>d{4}-d{1,2}-d{1,2})s+(?P<kundnummer>d+)")
match = reobj.search(input)
if match:
result1 = match.group("datum")
result2 = match.group("kundnummer")
I suggest something like this:
reobj = re.compile(r"(?P<datum>d{4}-d{1,2}-d{1,2})s+(?P<kundnummer>d+)")
match = reobj.search(input)
if match:
result1 = match.group("datum")
result2 = match.group("kundnummer")
answered 2 days ago
Andie2302
3,26221436
3,26221436
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53945344%2fhow-can-i-get-the-regex-equivalent-if-the-expression-are-in-separate-lines%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
If all your data contains is those two lines and the content of the first line is constant, don't bother matching the first line at all. Match something that conforms to the format of the second line and extracts the relevant parts, like
([12]d{3}-0[1-9]|1[0-2]-0[1-9]|[12]d|3[01])s(d+)
and replace byDatum 1nKundnummer 2
– Aaron
2 days ago
Oh and the big problem with the regex you tried is that while it would perfectly match the format of your expected output, what you need it to match is the format of your input.
– Aaron
2 days ago
How can I replace it..Can you just explain that!
– RAM SHANKER G
2 days ago
Sorry, I don't do much python. Look into the "re" module's documentation.
– Aaron
2 days ago
Why would you use regex for this? It seems like overkill.
– trincot
2 days ago