<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[עזרה בביטוי רגולארי]]></title><description><![CDATA[<p dir="auto">יש לי טקסט בפורמט מדיה ויקי, ואני צריך לשמור טקסט שנמצא בתוך תבנית בשם "הערה" במילון ולהכניס בטקסט עצמו את המפתח, הבעיה מתחילה כשיש בתוך התבנית פרמטר שהוא תבנית נוספת, לדוג':</p>
<pre><code>{{הערה|{{אישי ישראל|רבי אברהם יצחק משכיל לאיתן}}.}}
</code></pre>
<p dir="auto">איך אני יכול לתפוס את התבנית כולה?<br />
יצויין שיכולות להיות רמות קינון עמוקות יותר.</p>
]]></description><link>https://tchumim.com/topic/16786/עזרה-בביטוי-רגולארי</link><generator>RSS for Node</generator><lastBuildDate>Sat, 13 Jun 2026 02:13:44 GMT</lastBuildDate><atom:link href="https://tchumim.com/topic/16786.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 16 Dec 2024 06:00:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Tue, 17 Dec 2024 10:53:50 GMT]]></title><description><![CDATA[<p dir="auto">א. בשלושת הפונקציות, אתה בודק האם האובייקט ריק (כל הif בקוד, כולם). זה מיותר, אתה יכול ישירות לבצע את הפעולה, אם הוא ריק תהיה תוצאה ריקה ואפס סיבובים בלולאה<br />
ב. בפונקציה filter_templates אתה משתמש בלולאת for אלגנטית. בשני הפונקציות האחרות אתה משתמש בwhile, אתה יכול להשתמש באותה לולאת for<br />
ג. בפונקציה filter_templates אתה אוסף את התוצאות לתוך מערך (או list לא יודע איך קוראים לזה בפייתון)<br />
אתה יכול להשתמש עם yield, זה פשוט מזין כל תוצאה בתורה ללולאת for שאתה כותב בפונקציה הקוראת</p>
<p dir="auto">דוגמה לקוד של filter_templates  אחרי התיקונים הנ"ל:</p>
<pre><code>def filter_templates(string):
    parsed = mwparserfromhell.parse(string)
    for template in parsed.filter_templates(parsed.RECURSE_OTHERS):
        template_str = "".join([str(param) for param in template.params])
        yield [str(template), template.name.strip(), template_str]
</code></pre>
<p dir="auto">כמו"כ כדאי להשתמש עם אובייקטים במקום מערך בתוצאה, משהו כזה:</p>
<pre><code>def filter_templates(string):
    parsed = mwparserfromhell.parse(string)
    for template in parsed.filter_templates(parsed.RECURSE_OTHERS):
        template_str = "".join(map(str, template.params))
        yield {
            "template": str(template),
            "name": template.name.strip(),
            "template_str": template_str,
        }
</code></pre>
<p dir="auto">ובקוד המשתמש צריך לפנות לi["name"] במקום למיקום מספרי.</p>
<p dir="auto">לגופה של לוגיקה אני אל יודע בדיוק אם הקוד עושה מה שצריך, זה אתה יכול לבדוק יותר טוב ממני.</p>
]]></description><link>https://tchumim.com/post/165234</link><guid isPermaLink="true">https://tchumim.com/post/165234</guid><dc:creator><![CDATA[dovid]]></dc:creator><pubDate>Tue, 17 Dec 2024 10:53:50 GMT</pubDate></item><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Tue, 17 Dec 2024 08:28:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dovid">@<bdi>dovid</bdi></a><br />
אשמח.</p>
]]></description><link>https://tchumim.com/post/165233</link><guid isPermaLink="true">https://tchumim.com/post/165233</guid><dc:creator><![CDATA[האדם החושב]]></dc:creator><pubDate>Tue, 17 Dec 2024 08:28:41 GMT</pubDate></item><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Tue, 17 Dec 2024 07:03:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%94%D7%90%D7%93%D7%9D-%D7%94%D7%97%D7%95%D7%A9%D7%91">@<bdi>האדם-החושב</bdi></a> זה הכי טוב.<br />
האם אתה רוצה הערות על הקוד?</p>
]]></description><link>https://tchumim.com/post/165232</link><guid isPermaLink="true">https://tchumim.com/post/165232</guid><dc:creator><![CDATA[dovid]]></dc:creator><pubDate>Tue, 17 Dec 2024 07:03:11 GMT</pubDate></item><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Tue, 17 Dec 2024 01:49:25 GMT]]></title><description><![CDATA[<p dir="auto">תודה לכל העונים.<br />
בפועל השתמשתי בספריית mwparserfromhell</p>
<pre><code>import re
import mwparserfromhell

def filter_templates(string):
    string_2 = []
    parsed = mwparserfromhell.parse(string)
    templates = parsed.filter_templates(parsed.RECURSE_OTHERS)
    if templates:
        for template in templates:
            if template.params:
                template_str = "".join([str(param) for param in template.params])
            else:
                template_str = ""
            string_2.append([str(template),template.name, template_str])
        return string_2
    else:
        return False
    
def clean_comment(comment):
    while True:
        replace = filter_templates(comment)
        if not replace:
            break
        for i in replace:
            rp = i[2]
            comment = comment.replace(i[0], rp)
    return comment

def remove_templates(wikitext):
    dict_comments = {}
    sup = 0
    while True:
        replace = filter_templates(wikitext)
        if not replace:
            break
        for i in replace:
            if i[1].strip() == "הערה":
                sup += 1
                dict_comments[sup] = clean_comment(i[2])
                rp = f'&lt;sup style="color: gray;"&gt;{sup}&lt;/sup&gt;'
            elif i[1].strip() == "ש":
                rp = "\n"
            else:
                rp = i[2]
            wikitext = wikitext.replace(i[0], rp)
    counter = 0
    sorted_dict = {}
    for num in re.findall(r'&lt;sup style="color: gray;"&gt;(\d+)&lt;/sup&gt;', wikitext):
        counter += 1
        wikitext = wikitext.replace(rf'&lt;sup style="color: gray;"&gt;{num}&lt;/sup&gt;', rf'&lt;sup style="color: gray;"&gt;{counter}&lt;/sup&gt;')
        sorted_dict[counter] = dict_comments[int(num)]
    return wikitext, sorted_dict
</code></pre>
]]></description><link>https://tchumim.com/post/165231</link><guid isPermaLink="true">https://tchumim.com/post/165231</guid><dc:creator><![CDATA[האדם החושב]]></dc:creator><pubDate>Tue, 17 Dec 2024 01:49:25 GMT</pubDate></item><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Mon, 16 Dec 2024 19:53:02 GMT]]></title><description><![CDATA[<p dir="auto">לגופו של עניין:<br />
אין לי היכרות עם מדיה-ויקי או עם הפורמט שלו, והמשמעות של הנקודה ושל ה-"|" אינן ברורות לי דיו. עם זאת, אני מניח שמדובר בפורמט מסודר עם כללים, ואם תלמד איך הוא בנוי, אני בטוח שתוכל לנתח כל טקסט בסגנון הזה די בקלות הכל שאלה של כמה זמן אתה מוכן להשקיע.</p>
<p dir="auto">אם נתעלם לרגע מהנקודה ומה-"|", נוכל להשתמש בשילוב של רג'קס וקוד רקורסיבי עם מבנה נתונים פשוט (<a class="plugin-mentions-user plugin-mentions-a" href="/user/dovid">@<bdi>dovid</bdi></a> רמז לזה כבר).<br />
<em><strong>אבל חשוב לציין שזה בהנחה שהטקסט לא מכיל שגיאות או אי-עקביות – אחרת זה כאב ראש * 20.</strong></em><br />
להלן דוגמא ב-C# שהזנתי ל-LinqPad, הקוד הינו סקיצה בעלמא תוכל לשפר אותו כיד ה' הטובה עליך:</p>
<pre><code>void Main()
{
    string content = "{{some content{{some more content{{some more content{{some more content}}}}}}}}";
        var root = new NestedContent(content, null);
        root.Dump(); // Dump the root object to view its structure
}

class NestedContent
{
    public string Content { get; set; }
    public NestedContent Child { get; set; }

    public NestedContent(string content, NestedContent parent)
    {
        // Trim only the outermost braces (single pair at start and end)
        if (content.StartsWith("{{") &amp;&amp; content.EndsWith("}}"))
        {
            content = content.Substring(2, content.Length - 4);
        }

        var match = Regex.Match(content, @"\{\{.*\}\}");
        if (match.Success)
        {
            string nestedContent = match.Value;
            this.Content = content.Replace(nestedContent, ""); // Replace inner braces content
            Child = new NestedContent(nestedContent, this); // Recursive call for nested content
        }
        else
        {
            this.Content = content; // Set content when no more nested structures
        }
    }
}

</code></pre>
<p dir="auto">והתוצאה:</p>
<p dir="auto"><img src="/assets/uploads/files/1734378334658-d7b843dd-ffba-4875-bdb5-2e95805263aa-image.png" alt="d7b843dd-ffba-4875-bdb5-2e95805263aa-image.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://tchumim.com/post/165219</link><guid isPermaLink="true">https://tchumim.com/post/165219</guid><dc:creator><![CDATA[pcinfogmach]]></dc:creator><pubDate>Mon, 16 Dec 2024 19:53:02 GMT</pubDate></item><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Mon, 16 Dec 2024 19:22:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%94%D7%90%D7%93%D7%9D-%D7%94%D7%97%D7%95%D7%A9%D7%91">@<bdi>האדם-החושב</bdi></a><br />
זה יפה שכולם פיענחו מה אתה רוצה לי זה לקח קצת זמן עד שקלטתי שאני צריך לקרוא את מה שכתבת בפנים בקונטקסט של הכותרת ולנחש שבעצם אתה שואל איך לעשות רגקס שיפתור את הבעיה.<br />
אתה כותב הבעיה מתחילה מבלי לפרט מה עשית לפני שהבעיה התחילה.</p>
<p dir="auto">אישית לא הייתי שואל ככה אלא איך לפתור את הבעיה והאם רגקס הוא כיוון טוב.</p>
]]></description><link>https://tchumim.com/post/165218</link><guid isPermaLink="true">https://tchumim.com/post/165218</guid><dc:creator><![CDATA[pcinfogmach]]></dc:creator><pubDate>Mon, 16 Dec 2024 19:22:26 GMT</pubDate></item><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Mon, 16 Dec 2024 16:02:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dovid">@<bdi>dovid</bdi></a> כתב ב<a href="/post/165203">עזרה בביטוי רגולארי</a>:</p>
<blockquote>
<p dir="auto">REGEX לא טוב לביטויים רקורסיביים.</p>
</blockquote>
<p dir="auto">"לא טוב" זו הגדרה עדינה...<br />
<a href="https://stackstatus.tumblr.com/post/147710624694/outage-postmortem-july-20-2016" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackstatus.tumblr.com/post/147710624694/outage-postmortem-july-20-2016</a><br />
<a href="https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/" target="_blank" rel="noopener noreferrer nofollow ugc">https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/</a><br />
<a href="https://www.crowdstrike.com/wp-content/uploads/2024/08/Channel-File-291-Incident-Root-Cause-Analysis-08.06.2024.pdf" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.crowdstrike.com/wp-content/uploads/2024/08/Channel-File-291-Incident-Root-Cause-Analysis-08.06.2024.pdf</a></p>
<p dir="auto">מבחינתי Regex הוא כמו אנטיביוטיקה, אני משתמש בו בזהירות רק כשאין לי ברירה.</p>
]]></description><link>https://tchumim.com/post/165217</link><guid isPermaLink="true">https://tchumim.com/post/165217</guid><dc:creator><![CDATA[OdedDvir]]></dc:creator><pubDate>Mon, 16 Dec 2024 16:02:24 GMT</pubDate></item><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Mon, 16 Dec 2024 07:41:18 GMT]]></title><description><![CDATA[<p dir="auto">אני חייב לציין שתשובתי לא כנה לגמרי,<br />
כפי שהעירו לי בפרטי יש דרכים בREGEX להתמודד עם רקורסיביות וידעתי זאת,<br />
אלא שא. זה לא מומלץ וזה עניתי נכון, ב. אני לא יודע להשתמש בזה - וזה כבר "נגיעה" בתשובה שלי.<br />
אני שם כאן מ"מ לספורטיביים:<br />
<a href="https://stackoverflow.com/questions/17003799/what-are-regular-expression-balancing-groups" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/17003799/what-are-regular-expression-balancing-groups</a></p>
]]></description><link>https://tchumim.com/post/165207</link><guid isPermaLink="true">https://tchumim.com/post/165207</guid><dc:creator><![CDATA[dovid]]></dc:creator><pubDate>Mon, 16 Dec 2024 07:41:18 GMT</pubDate></item><item><title><![CDATA[Reply to עזרה בביטוי רגולארי on Mon, 16 Dec 2024 07:11:08 GMT]]></title><description><![CDATA[<p dir="auto">REGEX לא טוב לביטויים רקורסיביים.<br />
HTML המפורסמת היא בעצם תבנית רקורסיבית, אם תכתוב בגוגל regex html תקבל כמה שיעורי השקפה בנושא.<br />
באיזה שפה אתה כותב? אתה צריך לעשות חיפוש של {{ הראשון, ומשמה להעלות מונה כל פעם באיתור ולהפחית כל פעם שיש }}. כשאתה מגיע ל0 אתה צריך לעצור, כי קיבלת את כל הביטוי.<br />
יש עוד דרכים יפות לעשות זאת, REGEX לא מתאים (אלא"כ משלבים אותו עם לולאה).</p>
]]></description><link>https://tchumim.com/post/165203</link><guid isPermaLink="true">https://tchumim.com/post/165203</guid><dc:creator><![CDATA[dovid]]></dc:creator><pubDate>Mon, 16 Dec 2024 07:11:08 GMT</pubDate></item></channel></rss>