I have the following text, stored in a dictionary entry called
info['growing_info']
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
I want a regular expression that will return...
>>>seed_spacing.group(1)
>>>1
>>>seed_spacing.group(2)
>>>"
My current attempt is
seed_spacing = re.search(r'(?:Sow|sow|transplant|Transplant).+(?!rows).+([\d]+)(\'|") apart',info['growing_info'])
which is not returning the results I want. I know that the loose use of the dot character is detrimental, but I'm not sure how to proceed.
You can use this regex:
re.search(r'(?:sow|transplant).+?(\d+)(\'|") apart', info['growing_info'], re.I)
regex101 demo
I ran some modifications, namely:
(?:sow|transplant)(?!rows).+; it didn't do anything and changed .+ to .+? for lazy match.If you want to absolutely avoid having rows in the matched text, use this instead:
re.search(r'(?:sow|transplant)(?:(?!rows).)+(\d+)(\'|") apart', info['growing_info'], re.I)
(?:(?!rows).) will match any character except rows.
.+(?!rows).+
Does not work because (?!rows) is not checking every character matched by any of the .+. Here's how it works (the bold part indicates what has been matched so far):
The regex attempts to match (?:sow|transplant) and finds it
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
Next comes .+ and this means, match every character (greedily):
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
Next comes (?!rows). The regex checks and sees that at the end, there is no rows.
Next comes .+. The regex has to backtrack 1 character to allow this match:
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
Then matches the last character with .+
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
Next is (\d+), so the regex looks for a digit, but can't find any. Since it cannot backtrack with the second .+, it will backtrack from the first .+:
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
Check (?!rows). All is clear, because there's no rows ahead. It matches the space with the second .+, then the number with (\d+) and then the quote.
(?:(?!rows).)
Here's how it works now:
The regex attempts to match (?:sow|transplant) and finds it
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
Next comes (?:(?!rows).) and this means, match one character and make sure there's no rows ahead each character (let's say it matched two characters with the + because space cannot be made bold):
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
Since there is no rows, continue with a repeat of this group. This continues until:
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
Now it cannot move forward because there's rows ahead. It thus proceeds with the other parts of the regex: (\d+). It thus backtracks to:
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
And matches (\d+)(\'|") as well. However, there's no apart to match, and therefore continues to backtrack until (\d+)(\'|") apart is matched:
Radishes require friable, well drained soils with a pH range of 5.8-6.8. Sow at any time during the season, beginning in early spring. Use 2-3" wide bands, seeds about 3/4-1" apart (about 35 seeds/ft. except 10 seeds/ft. for 624 Red Meat and 616 Nero Tondo), 1/2" deep, rows 1' apart, or any row or bedding scheme that will eliminate unplanted ground to discourage weeds. Radishes are adversely affected by hot, dry weather. They remain in prime condition only a few days and should be grown rapidly with plenty of moisture to be mild, tender, and attractive. If growth is checked, roots may become tough, pithy, and too spicy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With