The class attribute is not always in the same place.
Class attributes will have the form of:
the word class
optionally a space.
an equals sign
optionally another space.
a string, encapsulated either with a pair of single quotes, or a pair of double quotes. (If you want to specify that your code only finds double-quoted strings, so be it.)
So; give me a pattern that says the following.
Inside a table tag, find three subpatterns: Everything before the class attribute, the class attribute, and then everything after the class attribute.
(You had it nearly right the first time. You need to capture all 3 subpatterns, and they need to capture as many characters as necessary, non-greedily.)
So; you’ve now got the building blocks to build your replacement text with.
Your replacement string is the string <table, the first subpattern, the string class=", your replacement class(es), the string ", the third subpattern, and then the string >
So you’ve got the blocks; your replacement string at that point would be "\1babs\3" (“the first subpattern, then your replacement, then the third subpattern”)
That first .* should be lazy instead of greedy like it is now. Otherwise it might just match the class of a differt tag that follows the table.
Also, the last construct can be a bit more efficient like this ([^"]*)" - which means, 0 or more times anything but a double quote, followed by a double quote.
So in total it would become (<table.*?class=")([^"]*)" and then the replacement would become \1babs".
I am always really happy if you are responding. Since you give a lot of good advice (Thank you for it ).
What if you want to change only the classes in a PHP preg_replace. (Since that the thing is I wanted to do in the First place). (<table.class=")(.?)(“.*>) in the middle, I group the classes and replace them. with another class. But (<table.*?class=")([^"]*) has only two selectors (everything between <table *** class=”). And the second I not really get.
Because your class is greedy (*), it will consume all characters until it sees a double-quote, but it will not consume said double quote; that said, the way you have written it, [^"]*\w will never match anything, because you’re saying "Consume everything until you see a ", and then followed by a whitespace. But there isnt a whitespace following it - it will always be a double quote.
Note that when Remon give you the string, he said (<table.*?class=")([^"]*)" . That last " is not a typo.