Python 3.7: Using regex on a string with whitespace in the beginning
up vote
0
down vote
favorite
I have lines in a text file that I need to parse with regular expressions and assign numbers to variables. Here is an example of what the lines looks like
1 35 A K 0 0 182
So there is whitespace in the beginning of the string, and whenever I use this website (https://pythex.org/) it does not seem to match anything when I use the regular expression
^s+d+s+d+swsws+ds+ds+d
How can I use regular expressions to assign 182 to a variable?
regex python-3.7
add a comment |
up vote
0
down vote
favorite
I have lines in a text file that I need to parse with regular expressions and assign numbers to variables. Here is an example of what the lines looks like
1 35 A K 0 0 182
So there is whitespace in the beginning of the string, and whenever I use this website (https://pythex.org/) it does not seem to match anything when I use the regular expression
^s+d+s+d+swsws+ds+ds+d
How can I use regular expressions to assign 182 to a variable?
regex python-3.7
4
You're missing a backslash on the firsts+. Try changing it to^s+d+s+d+swsws+ds+ds+d
– Davіd
Nov 8 at 2:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have lines in a text file that I need to parse with regular expressions and assign numbers to variables. Here is an example of what the lines looks like
1 35 A K 0 0 182
So there is whitespace in the beginning of the string, and whenever I use this website (https://pythex.org/) it does not seem to match anything when I use the regular expression
^s+d+s+d+swsws+ds+ds+d
How can I use regular expressions to assign 182 to a variable?
regex python-3.7
I have lines in a text file that I need to parse with regular expressions and assign numbers to variables. Here is an example of what the lines looks like
1 35 A K 0 0 182
So there is whitespace in the beginning of the string, and whenever I use this website (https://pythex.org/) it does not seem to match anything when I use the regular expression
^s+d+s+d+swsws+ds+ds+d
How can I use regular expressions to assign 182 to a variable?
regex python-3.7
regex python-3.7
edited Nov 8 at 2:45
Barmar
413k34239340
413k34239340
asked Nov 8 at 2:28
flannel_bioinformatician
385
385
4
You're missing a backslash on the firsts+. Try changing it to^s+d+s+d+swsws+ds+ds+d
– Davіd
Nov 8 at 2:35
add a comment |
4
You're missing a backslash on the firsts+. Try changing it to^s+d+s+d+swsws+ds+ds+d
– Davіd
Nov 8 at 2:35
4
4
You're missing a backslash on the first
s+. Try changing it to ^s+d+s+d+swsws+ds+ds+d– Davіd
Nov 8 at 2:35
You're missing a backslash on the first
s+. Try changing it to ^s+d+s+d+swsws+ds+ds+d– Davіd
Nov 8 at 2:35
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can use the following regex.
^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)
You can put the last number in a group which you can capture in your code.
Following is a test python script.
import re
regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"
test_str = " 1 35 A K 0 0 182"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
Output:
Match 1 was found at 0-40: 1 35 A K 0 0 182
Group 1 found at 37-40: 182
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use the following regex.
^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)
You can put the last number in a group which you can capture in your code.
Following is a test python script.
import re
regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"
test_str = " 1 35 A K 0 0 182"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
Output:
Match 1 was found at 0-40: 1 35 A K 0 0 182
Group 1 found at 37-40: 182
add a comment |
up vote
0
down vote
You can use the following regex.
^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)
You can put the last number in a group which you can capture in your code.
Following is a test python script.
import re
regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"
test_str = " 1 35 A K 0 0 182"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
Output:
Match 1 was found at 0-40: 1 35 A K 0 0 182
Group 1 found at 37-40: 182
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use the following regex.
^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)
You can put the last number in a group which you can capture in your code.
Following is a test python script.
import re
regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"
test_str = " 1 35 A K 0 0 182"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
Output:
Match 1 was found at 0-40: 1 35 A K 0 0 182
Group 1 found at 37-40: 182
You can use the following regex.
^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)
You can put the last number in a group which you can capture in your code.
Following is a test python script.
import re
regex = r"^s+d+s+d+s+w+s+w+s+d+s+d+s+(d+)"
test_str = " 1 35 A K 0 0 182"
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
Output:
Match 1 was found at 0-40: 1 35 A K 0 0 182
Group 1 found at 37-40: 182
answered Nov 8 at 3:16
Bilesh Ganguly
1,72811533
1,72811533
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%2f53200705%2fpython-3-7-using-regex-on-a-string-with-whitespace-in-the-beginning%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
4
You're missing a backslash on the first
s+. Try changing it to^s+d+s+d+swsws+ds+ds+d– Davіd
Nov 8 at 2:35