vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   help with a regular expression please (https://vborg.vbsupport.ru/showthread.php?t=102207)

akanevsky 12-04-2005 02:14 PM

help with a regular expression please
 
Say I have a string:
x!!x bbbbbdddd x!!x bbbbaaaaddddd x!!x bbbbcccccddddd
How do I match the bbbb(.*)dddd that do NOT have an "x!!x" substring between them? Don't ask what for, just tell me how do to such regex. explode() is not an option for my purposes. Thanks.

Ideally, it would be something like ([^x!!x]*), but that won't work for some reason.

Andreas 12-04-2005 02:36 PM

lookahead/lookbehind assertions

akanevsky 12-04-2005 02:45 PM

Example plz?

Andreas 12-04-2005 02:50 PM

> x!!x bbbbbdddd x!!x bbbbaaaaddddd x!!x bbbbcccccddddd
From this string, what would be your desired match(es)?

akanevsky 12-04-2005 08:06 PM

Hmm..
I just want to end up having bbbb moved as towards the closest ddddd, so that the result would be:

x!!x bbbbdddd x!!x aaaabbbbddddd x!!x cccccbbbbddddd

EDIT: Actually, nevermind. That won't work as desired because the corresponding bbbb and dddd could have inner pairs of x!!x between them, which would give an undesired result. So nvm... :P

Andreas 12-04-2005 09:32 PM

PHP Code:

preg_replace('/bbbb(.*)dddd/U''$1bbbbdddd''x!!x bbbbbdddd x!!x bbbbaaaaddddd x!!x bbbbcccccddddd'); 

?

akanevsky 12-04-2005 09:41 PM

Yes, that works. However, as I said, it could happen like.. let me explain.

<bbbb <bbbbaaaadddd> dddd>

So here, my result would have to be:
< <aaaabbbbdddd> bbbbdddd>
However, because there is another <....> inside the outer <....>, there is no way to see the the first bbbb belongs to the latest dddd. Or is there? :P

Andreas 12-04-2005 10:04 PM

<Disclaimer>
This Code is utterly bad
</Disclaimer>

PHP Code:

function recurse_replace($str)
{
    if (
preg_match('/bbbb(.*)dddd/'$str))
    {
        return 
preg_replace('/bbbb(.*)dddd/eS'"recurse_replace('\\1') . 'bbbbdddd'"$str);
    }
    else
    {
        return 
$str;
    }



akanevsky 12-04-2005 10:15 PM

Why is it utterly bad? Because of performance?

Andreas 12-04-2005 10:24 PM

Because it is recursive and not checked ;)


All times are GMT. The time now is 03:15 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01839 seconds
  • Memory Usage 1,728KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete