How to remove white spaces only in that part between quotation marks
I have the following string. I have to remove white spaces only in the part that is between single quotations. Rest of the part should be intact in the line,
+amk=0 nog = 0 nf=1 par=1 mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)' sca=0 scb=0 scc=0 pj='2* ((w+7.61e-6) + (l+8.32e-6 ))'
So the output should be
+amk=0 nog = 0 nf=1 par=1 mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)' sca=0 scb=0 scc=0 pj='2*((w+7.61e-6)+(l+8.32e-6))'
Is it possible to do this with a single Regex statement ? or needed multiple lines ?
python
add a comment |
I have the following string. I have to remove white spaces only in the part that is between single quotations. Rest of the part should be intact in the line,
+amk=0 nog = 0 nf=1 par=1 mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)' sca=0 scb=0 scc=0 pj='2* ((w+7.61e-6) + (l+8.32e-6 ))'
So the output should be
+amk=0 nog = 0 nf=1 par=1 mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)' sca=0 scb=0 scc=0 pj='2*((w+7.61e-6)+(l+8.32e-6))'
Is it possible to do this with a single Regex statement ? or needed multiple lines ?
python
try lookbehind and lookahead assertions to extract just that part. docs.python.org/3/library/re.html
– g_uint
Nov 19 '18 at 6:56
add a comment |
I have the following string. I have to remove white spaces only in the part that is between single quotations. Rest of the part should be intact in the line,
+amk=0 nog = 0 nf=1 par=1 mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)' sca=0 scb=0 scc=0 pj='2* ((w+7.61e-6) + (l+8.32e-6 ))'
So the output should be
+amk=0 nog = 0 nf=1 par=1 mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)' sca=0 scb=0 scc=0 pj='2*((w+7.61e-6)+(l+8.32e-6))'
Is it possible to do this with a single Regex statement ? or needed multiple lines ?
python
I have the following string. I have to remove white spaces only in the part that is between single quotations. Rest of the part should be intact in the line,
+amk=0 nog = 0 nf=1 par=1 mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)' sca=0 scb=0 scc=0 pj='2* ((w+7.61e-6) + (l+8.32e-6 ))'
So the output should be
+amk=0 nog = 0 nf=1 par=1 mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)' sca=0 scb=0 scc=0 pj='2*((w+7.61e-6)+(l+8.32e-6))'
Is it possible to do this with a single Regex statement ? or needed multiple lines ?
python
python
edited Nov 19 '18 at 6:53
Mayank Porwal
4,9222724
4,9222724
asked Nov 19 '18 at 6:51
hareeshhareesh
41
41
try lookbehind and lookahead assertions to extract just that part. docs.python.org/3/library/re.html
– g_uint
Nov 19 '18 at 6:56
add a comment |
try lookbehind and lookahead assertions to extract just that part. docs.python.org/3/library/re.html
– g_uint
Nov 19 '18 at 6:56
try lookbehind and lookahead assertions to extract just that part. docs.python.org/3/library/re.html
– g_uint
Nov 19 '18 at 6:56
try lookbehind and lookahead assertions to extract just that part. docs.python.org/3/library/re.html
– g_uint
Nov 19 '18 at 6:56
add a comment |
1 Answer
1
active
oldest
votes
As an alternative, you might want to consider finite state machine. I always forget the library, but it's super simple to create it on your own. Something like this:
def remove_quoted_whitespace(input_str):
"""
Remove space if it is quoted.
Examples
--------
>>> remove_quoted_whitespace("mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)'")
"mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)'"
"""
output =
is_quoted = False
quotechars = ["'"]
ignore_chars = [' ']
for c in input_str:
if (c in ignore_chars and not is_quoted) or c not in ignore_chars:
output.append(c)
if c in quotechars:
is_quoted = not is_quoted
return ''.join(output)
See also: Is list join really faster than string concatenation in python?
Thanks Martin. That was a great code. Where I can find more information to learn and code FSM in python
– hareesh
Nov 19 '18 at 7:56
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%2f53369629%2fhow-to-remove-white-spaces-only-in-that-part-between-quotation-marks%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 an alternative, you might want to consider finite state machine. I always forget the library, but it's super simple to create it on your own. Something like this:
def remove_quoted_whitespace(input_str):
"""
Remove space if it is quoted.
Examples
--------
>>> remove_quoted_whitespace("mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)'")
"mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)'"
"""
output =
is_quoted = False
quotechars = ["'"]
ignore_chars = [' ']
for c in input_str:
if (c in ignore_chars and not is_quoted) or c not in ignore_chars:
output.append(c)
if c in quotechars:
is_quoted = not is_quoted
return ''.join(output)
See also: Is list join really faster than string concatenation in python?
Thanks Martin. That was a great code. Where I can find more information to learn and code FSM in python
– hareesh
Nov 19 '18 at 7:56
add a comment |
As an alternative, you might want to consider finite state machine. I always forget the library, but it's super simple to create it on your own. Something like this:
def remove_quoted_whitespace(input_str):
"""
Remove space if it is quoted.
Examples
--------
>>> remove_quoted_whitespace("mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)'")
"mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)'"
"""
output =
is_quoted = False
quotechars = ["'"]
ignore_chars = [' ']
for c in input_str:
if (c in ignore_chars and not is_quoted) or c not in ignore_chars:
output.append(c)
if c in quotechars:
is_quoted = not is_quoted
return ''.join(output)
See also: Is list join really faster than string concatenation in python?
Thanks Martin. That was a great code. Where I can find more information to learn and code FSM in python
– hareesh
Nov 19 '18 at 7:56
add a comment |
As an alternative, you might want to consider finite state machine. I always forget the library, but it's super simple to create it on your own. Something like this:
def remove_quoted_whitespace(input_str):
"""
Remove space if it is quoted.
Examples
--------
>>> remove_quoted_whitespace("mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)'")
"mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)'"
"""
output =
is_quoted = False
quotechars = ["'"]
ignore_chars = [' ']
for c in input_str:
if (c in ignore_chars and not is_quoted) or c not in ignore_chars:
output.append(c)
if c in quotechars:
is_quoted = not is_quoted
return ''.join(output)
See also: Is list join really faster than string concatenation in python?
As an alternative, you might want to consider finite state machine. I always forget the library, but it's super simple to create it on your own. Something like this:
def remove_quoted_whitespace(input_str):
"""
Remove space if it is quoted.
Examples
--------
>>> remove_quoted_whitespace("mg =0.34e-6 sd='((nf != 1) ? (nf-1)) :0)'")
"mg =0.34e-6 sd='((nf!=1)?(nf-1)):0)'"
"""
output =
is_quoted = False
quotechars = ["'"]
ignore_chars = [' ']
for c in input_str:
if (c in ignore_chars and not is_quoted) or c not in ignore_chars:
output.append(c)
if c in quotechars:
is_quoted = not is_quoted
return ''.join(output)
See also: Is list join really faster than string concatenation in python?
edited Nov 19 '18 at 7:28
answered Nov 19 '18 at 7:06
Martin ThomaMartin Thoma
41.7k57299518
41.7k57299518
Thanks Martin. That was a great code. Where I can find more information to learn and code FSM in python
– hareesh
Nov 19 '18 at 7:56
add a comment |
Thanks Martin. That was a great code. Where I can find more information to learn and code FSM in python
– hareesh
Nov 19 '18 at 7:56
Thanks Martin. That was a great code. Where I can find more information to learn and code FSM in python
– hareesh
Nov 19 '18 at 7:56
Thanks Martin. That was a great code. Where I can find more information to learn and code FSM in python
– hareesh
Nov 19 '18 at 7:56
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%2f53369629%2fhow-to-remove-white-spaces-only-in-that-part-between-quotation-marks%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
try lookbehind and lookahead assertions to extract just that part. docs.python.org/3/library/re.html
– g_uint
Nov 19 '18 at 6:56