Converting unified version in python using regular expression












-2















I'm trying to convert a unified version format to the following in python



Examples:




  • <2.2.1 || >=4.0.0 < 4.1.9

    should be
    <2.2.1 || (>=4.0.0 && <4.1.9)


  • >=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12

    should be
    (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)


  • <2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6

    should be
    (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)



tried the following, it works but messy:



def rchop(thestring, ending):
if thestring.endswith(ending):
return thestring[:-len(ending)]
return thestring

ver = "<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6"
split_ver = ver.split('||')
list_data =
for version in split_ver:
version = version.rstrip()
version = version.lstrip()
vv = version.replace(" ", " && ")
list_data.append(vv)
print(list_data)

new_list =
for data in list_data:
if "&&" not in data and "=0" not in data and ">=" not in data:
new_data = "(>=0 && " + data + ")"
new_list.append(new_data)
else:
new_data1 = new_list.append("("+data+")")

final_list =
for items in new_list:
data = final_list.append(items + " || ")

now_data = [''.join(final_list[:])]
data1 = rchop(now_data[0], ' || ')
print(data1)









share|improve this question

























  • May we know what have you tried and what problems you faced?

    – boonwj
    Nov 13 '18 at 22:32











  • It is unclear what the requirement is here, would you please put some test code to reproduce the problem?

    – Saul Cruz
    Nov 13 '18 at 22:46











  • the requirements here is that the script I'm using produces buggy version format, so I'm trying to fix it using a regular expression by adding proper parentheses and proper && as the above conversions.

    – Jau L
    Nov 13 '18 at 22:52






  • 1





    I added the code @boonwj

    – Jau L
    Nov 13 '18 at 23:54
















-2















I'm trying to convert a unified version format to the following in python



Examples:




  • <2.2.1 || >=4.0.0 < 4.1.9

    should be
    <2.2.1 || (>=4.0.0 && <4.1.9)


  • >=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12

    should be
    (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)


  • <2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6

    should be
    (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)



tried the following, it works but messy:



def rchop(thestring, ending):
if thestring.endswith(ending):
return thestring[:-len(ending)]
return thestring

ver = "<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6"
split_ver = ver.split('||')
list_data =
for version in split_ver:
version = version.rstrip()
version = version.lstrip()
vv = version.replace(" ", " && ")
list_data.append(vv)
print(list_data)

new_list =
for data in list_data:
if "&&" not in data and "=0" not in data and ">=" not in data:
new_data = "(>=0 && " + data + ")"
new_list.append(new_data)
else:
new_data1 = new_list.append("("+data+")")

final_list =
for items in new_list:
data = final_list.append(items + " || ")

now_data = [''.join(final_list[:])]
data1 = rchop(now_data[0], ' || ')
print(data1)









share|improve this question

























  • May we know what have you tried and what problems you faced?

    – boonwj
    Nov 13 '18 at 22:32











  • It is unclear what the requirement is here, would you please put some test code to reproduce the problem?

    – Saul Cruz
    Nov 13 '18 at 22:46











  • the requirements here is that the script I'm using produces buggy version format, so I'm trying to fix it using a regular expression by adding proper parentheses and proper && as the above conversions.

    – Jau L
    Nov 13 '18 at 22:52






  • 1





    I added the code @boonwj

    – Jau L
    Nov 13 '18 at 23:54














-2












-2








-2


0






I'm trying to convert a unified version format to the following in python



Examples:




  • <2.2.1 || >=4.0.0 < 4.1.9

    should be
    <2.2.1 || (>=4.0.0 && <4.1.9)


  • >=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12

    should be
    (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)


  • <2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6

    should be
    (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)



tried the following, it works but messy:



def rchop(thestring, ending):
if thestring.endswith(ending):
return thestring[:-len(ending)]
return thestring

ver = "<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6"
split_ver = ver.split('||')
list_data =
for version in split_ver:
version = version.rstrip()
version = version.lstrip()
vv = version.replace(" ", " && ")
list_data.append(vv)
print(list_data)

new_list =
for data in list_data:
if "&&" not in data and "=0" not in data and ">=" not in data:
new_data = "(>=0 && " + data + ")"
new_list.append(new_data)
else:
new_data1 = new_list.append("("+data+")")

final_list =
for items in new_list:
data = final_list.append(items + " || ")

now_data = [''.join(final_list[:])]
data1 = rchop(now_data[0], ' || ')
print(data1)









share|improve this question
















I'm trying to convert a unified version format to the following in python



Examples:




  • <2.2.1 || >=4.0.0 < 4.1.9

    should be
    <2.2.1 || (>=4.0.0 && <4.1.9)


  • >=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12

    should be
    (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)


  • <2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6

    should be
    (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)



tried the following, it works but messy:



def rchop(thestring, ending):
if thestring.endswith(ending):
return thestring[:-len(ending)]
return thestring

ver = "<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6"
split_ver = ver.split('||')
list_data =
for version in split_ver:
version = version.rstrip()
version = version.lstrip()
vv = version.replace(" ", " && ")
list_data.append(vv)
print(list_data)

new_list =
for data in list_data:
if "&&" not in data and "=0" not in data and ">=" not in data:
new_data = "(>=0 && " + data + ")"
new_list.append(new_data)
else:
new_data1 = new_list.append("("+data+")")

final_list =
for items in new_list:
data = final_list.append(items + " || ")

now_data = [''.join(final_list[:])]
data1 = rchop(now_data[0], ' || ')
print(data1)






python regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 0:07







Jau L

















asked Nov 13 '18 at 22:12









Jau LJau L

3501413




3501413













  • May we know what have you tried and what problems you faced?

    – boonwj
    Nov 13 '18 at 22:32











  • It is unclear what the requirement is here, would you please put some test code to reproduce the problem?

    – Saul Cruz
    Nov 13 '18 at 22:46











  • the requirements here is that the script I'm using produces buggy version format, so I'm trying to fix it using a regular expression by adding proper parentheses and proper && as the above conversions.

    – Jau L
    Nov 13 '18 at 22:52






  • 1





    I added the code @boonwj

    – Jau L
    Nov 13 '18 at 23:54



















  • May we know what have you tried and what problems you faced?

    – boonwj
    Nov 13 '18 at 22:32











  • It is unclear what the requirement is here, would you please put some test code to reproduce the problem?

    – Saul Cruz
    Nov 13 '18 at 22:46











  • the requirements here is that the script I'm using produces buggy version format, so I'm trying to fix it using a regular expression by adding proper parentheses and proper && as the above conversions.

    – Jau L
    Nov 13 '18 at 22:52






  • 1





    I added the code @boonwj

    – Jau L
    Nov 13 '18 at 23:54

















May we know what have you tried and what problems you faced?

– boonwj
Nov 13 '18 at 22:32





May we know what have you tried and what problems you faced?

– boonwj
Nov 13 '18 at 22:32













It is unclear what the requirement is here, would you please put some test code to reproduce the problem?

– Saul Cruz
Nov 13 '18 at 22:46





It is unclear what the requirement is here, would you please put some test code to reproduce the problem?

– Saul Cruz
Nov 13 '18 at 22:46













the requirements here is that the script I'm using produces buggy version format, so I'm trying to fix it using a regular expression by adding proper parentheses and proper && as the above conversions.

– Jau L
Nov 13 '18 at 22:52





the requirements here is that the script I'm using produces buggy version format, so I'm trying to fix it using a regular expression by adding proper parentheses and proper && as the above conversions.

– Jau L
Nov 13 '18 at 22:52




1




1





I added the code @boonwj

– Jau L
Nov 13 '18 at 23:54





I added the code @boonwj

– Jau L
Nov 13 '18 at 23:54












1 Answer
1






active

oldest

votes


















1














Not sure you need regex for this. Seems like you could just split the strings a couple times, reformat the parts and then join them back together to match your example outputs (assuming that the example output where you show <2.2.1 || (>=4.0.0 && <4.1.9) is a typo and it should actually follow the pattern of the other similar example and be (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9) instead.



Maybe there are more edge cases that don't follow the example patterns, but the below should at least give you a simpler starting place to work from.



def version_formatter(text):
raw = [t.strip().split() for t in text.split('||')]
formatted = [f'({r[0]} && {r[1]})' if len(r) == 2 else f'(>=0 && {r[0]})' for r in raw]
return ' || '.join(formatted)

tests = ['<2.2.1 || >=4.0.0 <4.1.9', '>=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12', '<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6']

for test in tests:
result = version_formatter(test)
print(result)

# OUTPUT
# (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9)
# (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)
# (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)





share|improve this answer


























  • thanks a lot, @benvc. this works :)

    – Jau L
    Nov 15 '18 at 18:51











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290295%2fconverting-unified-version-in-python-using-regular-expression%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









1














Not sure you need regex for this. Seems like you could just split the strings a couple times, reformat the parts and then join them back together to match your example outputs (assuming that the example output where you show <2.2.1 || (>=4.0.0 && <4.1.9) is a typo and it should actually follow the pattern of the other similar example and be (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9) instead.



Maybe there are more edge cases that don't follow the example patterns, but the below should at least give you a simpler starting place to work from.



def version_formatter(text):
raw = [t.strip().split() for t in text.split('||')]
formatted = [f'({r[0]} && {r[1]})' if len(r) == 2 else f'(>=0 && {r[0]})' for r in raw]
return ' || '.join(formatted)

tests = ['<2.2.1 || >=4.0.0 <4.1.9', '>=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12', '<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6']

for test in tests:
result = version_formatter(test)
print(result)

# OUTPUT
# (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9)
# (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)
# (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)





share|improve this answer


























  • thanks a lot, @benvc. this works :)

    – Jau L
    Nov 15 '18 at 18:51
















1














Not sure you need regex for this. Seems like you could just split the strings a couple times, reformat the parts and then join them back together to match your example outputs (assuming that the example output where you show <2.2.1 || (>=4.0.0 && <4.1.9) is a typo and it should actually follow the pattern of the other similar example and be (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9) instead.



Maybe there are more edge cases that don't follow the example patterns, but the below should at least give you a simpler starting place to work from.



def version_formatter(text):
raw = [t.strip().split() for t in text.split('||')]
formatted = [f'({r[0]} && {r[1]})' if len(r) == 2 else f'(>=0 && {r[0]})' for r in raw]
return ' || '.join(formatted)

tests = ['<2.2.1 || >=4.0.0 <4.1.9', '>=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12', '<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6']

for test in tests:
result = version_formatter(test)
print(result)

# OUTPUT
# (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9)
# (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)
# (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)





share|improve this answer


























  • thanks a lot, @benvc. this works :)

    – Jau L
    Nov 15 '18 at 18:51














1












1








1







Not sure you need regex for this. Seems like you could just split the strings a couple times, reformat the parts and then join them back together to match your example outputs (assuming that the example output where you show <2.2.1 || (>=4.0.0 && <4.1.9) is a typo and it should actually follow the pattern of the other similar example and be (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9) instead.



Maybe there are more edge cases that don't follow the example patterns, but the below should at least give you a simpler starting place to work from.



def version_formatter(text):
raw = [t.strip().split() for t in text.split('||')]
formatted = [f'({r[0]} && {r[1]})' if len(r) == 2 else f'(>=0 && {r[0]})' for r in raw]
return ' || '.join(formatted)

tests = ['<2.2.1 || >=4.0.0 <4.1.9', '>=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12', '<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6']

for test in tests:
result = version_formatter(test)
print(result)

# OUTPUT
# (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9)
# (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)
# (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)





share|improve this answer















Not sure you need regex for this. Seems like you could just split the strings a couple times, reformat the parts and then join them back together to match your example outputs (assuming that the example output where you show <2.2.1 || (>=4.0.0 && <4.1.9) is a typo and it should actually follow the pattern of the other similar example and be (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9) instead.



Maybe there are more edge cases that don't follow the example patterns, but the below should at least give you a simpler starting place to work from.



def version_formatter(text):
raw = [t.strip().split() for t in text.split('||')]
formatted = [f'({r[0]} && {r[1]})' if len(r) == 2 else f'(>=0 && {r[0]})' for r in raw]
return ' || '.join(formatted)

tests = ['<2.2.1 || >=4.0.0 <4.1.9', '>=7.0.23 <7.0.91 || >=8.5.0 <8.5.34 || >=9.0.0 <9.0.12', '<2.7.9.4 || >=2.8.0 <2.8.11.2 || >=2.9.0 <2.9.6']

for test in tests:
result = version_formatter(test)
print(result)

# OUTPUT
# (>=0 && <2.2.1) || (>=4.0.0 && <4.1.9)
# (>=7.0.23 && <7.0.91) || (>=8.5.0 && <8.5.34) || (>=9.0.0 && <9.0.12)
# (>=0 && <2.7.9.4) || (>=2.8.0 && <2.8.11.2) || (>=2.9.0 && <2.9.6)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 13:12

























answered Nov 15 '18 at 4:09









benvcbenvc

5,1471623




5,1471623













  • thanks a lot, @benvc. this works :)

    – Jau L
    Nov 15 '18 at 18:51



















  • thanks a lot, @benvc. this works :)

    – Jau L
    Nov 15 '18 at 18:51

















thanks a lot, @benvc. this works :)

– Jau L
Nov 15 '18 at 18:51





thanks a lot, @benvc. this works :)

– Jau L
Nov 15 '18 at 18:51


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290295%2fconverting-unified-version-in-python-using-regular-expression%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values