PDA

View Full Version : سوال: عبارات منظم در C++‎



Anisi1371
سه شنبه 12 اردیبهشت 1391, 01:30 صبح
سلام دوستان
من مي خوام از عبارات منظم توي برنامم استفاده كنم اما نحوه الگوبنديش رو نمي دونم.
مثلا چطور ميشه همه عباراتي كه الگوي زير رو دارن جدا كرد (مي خوام متن بين تگ p رو بريزم توي آرايه)
من ااز الگوي زير استفاده كردم ولي جواب نميده!
^(<p class=\"authors\">)(.*)(</p>)$

<p class="authors"><a title="View content where Author is Vincent Baudoui" href="/content/?Author=Vincent+Baudoui">Vincent Baudoui</a>, <a title="View content where Author is Patricia Klotz" href="/content/?Author=Patricia+Klotz">Patricia Klotz</a>, <a title="View content where Author is Jean-Baptiste Hiriart-Urruty" href="/content/?Author=Jean-Baptiste+Hiriart-Urruty">Jean-Baptiste Hiriart-Urruty</a>, <a title="View content where Author is Sophie Jan" href="/content/?Author=Sophie+Jan">Sophie Jan</a> and <a title="View content where Author is Franck Morel" href="/content/?Author=Franck+Morel">Franck Morel</a></p>اگر لطف كنيد كلا نحوه الگوسازي رو توضيح بديد ممنون ميشم.

mehdi.mousavi
سه شنبه 12 اردیبهشت 1391, 10:48 صبح
سلام.
حقیقتش الان فرصت توضیح چگونگی نوشتن Pattern ها رو ندارم، اما در مورد سوالتون باید بدین شکل عمل کنید:

string input = "<p class=\"authors\"><a title=\"View content where Author is Vincent Baudoui\" href=\"/content/?Author=Vincent+Baudoui\">Vincent Baudoui</a>, <a title=\"View content where Author is Patricia Klotz\" href=\"/content/?Author=Patricia+Klotz\">Patricia Klotz</a>, <a title=\"View content where Author is Jean-Baptiste Hiriart-Urruty\" href=\"/content/?Author=Jean-Baptiste+Hiriart-Urruty\">Jean-Baptiste Hiriart-Urruty</a>, <a title=\"View content where Author is Sophie Jan\" href=\"/content/?Author=Sophie+Jan\">Sophie Jan</a> and <a title=\"View content where Author is Franck Morel\" href=\"/content/?Author=Franck+Morel\">Franck Morel</a></p>";
regex re("^<p class=\"authors\">(.*)</p>$");

smatch result;
regex_search(input, result, re);
for(size_t i = 0; i < result.size(); i++)
{
string capture = result[i];
}


موفق باشید.

Anisi1371
سه شنبه 12 اردیبهشت 1391, 13:10 عصر
ممنون ولي وقتي اين الگو بين يه عالمه متن ديگه باشه جواب نميده!
مثلا براي

string input = "grbttbtbbtbtbtbtb<p class=\"authors\"><a title=\"View content where Author is Vincent Baudoui\" href=\"/content/?Author=Vincent+Baudoui\">Vincent Baudoui</a>, <a title=\"View content where Author is Patricia Klotz\" href=\"/content/?Author=Patricia+Klotz\">Patricia Klotz</a>, <a title=\"View content where Author is Jean-Baptiste Hiriart-Urruty\" href=\"/content/?Author=Jean-Baptiste+Hiriart-Urruty\">Jean-Baptiste Hiriart-Urruty</a>, <a title=\"View content where Author is Sophie Jan\" href=\"/content/?Author=Sophie+Jan\">Sophie Jan</a> and <a title=\"View content where Author is Franck Morel\" href=\"/content/?Author=Franck+Morel\">Franck Morel</a></p>";

mehdi.mousavi
سه شنبه 12 اردیبهشت 1391, 13:22 عصر
ممنون ولي وقتي اين الگو بين يه عالمه متن ديگه باشه جواب نميده!
مثلا براي

string input = "grbttbtbbtbtbtbtb<p class=\"authors\"><a title=\"View content where Author is Vincent Baudoui\" href=\"/content/?Author=Vincent+Baudoui\">Vincent Baudoui</a>, <a title=\"View content where Author is Patricia Klotz\" href=\"/content/?Author=Patricia+Klotz\">Patricia Klotz</a>, <a title=\"View content where Author is Jean-Baptiste Hiriart-Urruty\" href=\"/content/?Author=Jean-Baptiste+Hiriart-Urruty\">Jean-Baptiste Hiriart-Urruty</a>, <a title=\"View content where Author is Sophie Jan\" href=\"/content/?Author=Sophie+Jan\">Sophie Jan</a> and <a title=\"View content where Author is Franck Morel\" href=\"/content/?Author=Franck+Morel\">Franck Morel</a></p>";



خوب اینطوری که شما صورت مساله رو تغییر دادید. :) اگر <p> خودش در درون یک Tag دیگه باشه، مشخصه که Pattern نوشته شده کاری انجام نخواهد داد...
در اینصورت، علامت ^ و $ رو از ابتدا و انتهای Pattern حذف کنید تا جواب بگیرید. علامت ^ نشون میده که باید <p در ابتدای خط باشه، و علامت $ هم داره میگه
</p> باید در انتهای خط باشه... پس حذف این دو، مشکل رو رفع میکنه.

موفق باشید.