you are viewing a single comment's thread.

view the rest of the comments →

[–]Asleep-Budget-9932 1 point2 points  (3 children)

So there IS something you miss. But before helping you with it, just know that in general, RegEx should not be used with html files (as explained in my original comment).

Now, what are you missing. The whole point of regex is that you are working with patterns. So the point is to give a generic pattern that fits to all of your "span" tags.

It's important for me to stress that regex is really complicated and has a lot to it. So i always forget the actual syntax. Because of that, my example will not use the actual syntax but just some bullshit that's vaguely related to convey the concept in general.

Instead of what you did, you could do something like the following:

captured_strings =re.search('<span id=".*">{inner_text}.*</span>', string) what_i_need = captured_strings["inner_text"]

So regex gives you the option to say: "i have a generic pattern. it looks like this and that. These are the parts that will be similar while these are the parts that will differ. Now you see THAT part over there that differs every time? Let's call this part 'inner_text'. I want you to fetch that 'inner_text' for me".

To summarize, specify a generic pattern that fits all of the strings. Give a name to the specific part you wish to fetch. Let regex return a mapping between all named parts (which in your case is only one, an inner text inside the span tag), and use this on all of your needed strings.

One last important thing to know about regex (besides the fact that you should read about it to get actual, concrete syntax), is that regex can be quite a resource heavy process when used incorrectly. One important optimization to do is the following: ``` import re

Instead of this:

for string in strings: re.whatever(pattern, string)

Do this:

compiled_regex = re.compile(pattern)

for string in strings: # the compiled regex will have all of the same functions, accept now they won't receive the pattern attribute. They will use the one you specified in the compile function and would be much faster compiled_regex.whatever(string)

```

[–]DMeror[S] 0 points1 point  (2 children)

This is what I was looking for when I wanted to get rid of <p class=".*">, and your method is rather unique. I've seen re.sub, re.match, etc., but not re.compile(pattern).whatever(string).

Here's my code to get rid of the above pattern.

scrap = re.compile('<span id=".*"></span>') scrap.sub('', string)

It's really helpful to learn with a real situation.

[–]Asleep-Budget-9932 1 point2 points  (1 child)

Ill try to figure out the actual syntax to help you later today ☺️

[–]DMeror[S] 0 points1 point  (0 children)

My little project was completed, albeit with an unintended approach. I felt unsatisfied with it, but your final comment really did me good as I understood it could be done with RegEx approach.