Jump to content

ragax

Members
  • Posts

    186
  • Joined

  • Last visited

Everything posted by ragax

  1. Hi Sean, I'm not sure I understand all the details of your request, but to get you started, I've made you a pattern that captures the code. Can you run the code below, confirm that this is what you want to capture, and explain where you want to go from there? <?php $text='<code><?php echo \"This is the code\"; echo \"<br/>\"; echo \"and some more code\"; ?></code>;'; $pattern='%(?s)<code>((??!</code>).)*)</code>%'; preg_match($pattern,$text,$result); echo 'capture: '.htmlentities($result[1]).'<br />'; ?> In the next stage, we'll probably want to use a preg_replace. Wishing you a fun day.
  2. Hi Debbie, Here is an expression that matches your password requirement. I have unrolled it in "comment mode" so you can see what each line does. That way you can understand how the lookahead works. (?x) # comment mode ^ # beg of string anchor (?=[\d\D]{8,20}$) # lookahead: between 8 and 20 digits or non-digits, then the end of the string (?=.*[A-Z]) # lookahead: an upper-case character (?=.*[a-z]) # lookahead: a lower-case character (?=.*[0-9]) # lookahead: a number (?=.*[*?!&_-]) # lookahead: a special character .* # match anything $ # end of string anchor You can actually paste that directly into your PHP, or you can bring it back to one line and remove the comments. I would recommend leaving the comments. I wasn't sure what you wanted to include in the special characters, so have a look on that line and add whatever you like between the brackets (you would have to escape a backslash). Here is an example of use in a PHP program (tested): <?php $passes=array('2_Short','Need_A_Number','Valid_Pass99','This_1_is_going_to_be_too_long'); foreach ($passes as $pass) if (preg_match('/(?x) # comment mode ^ # beg of string anchor (?=[\d\D]{8,20}$) # lookahead: between 8 and 20 digits or non-digits, then the end of the string (?=.*[A-Z]) # lookahead: an upper-case character (?=.*[a-z]) # lookahead: a lower-case character (?=.*[0-9]) # lookahead: a number (?=.*[*?!&_-]) # lookahead: a special character .* # match anything $ # end of string anchor /m', $pass)) echo 'Valid: '.$pass.'<br />'; else echo 'Invalid: '.$pass.'<br />'; ?> Don't forget the closing ?> as it is not showing inside the code box. Please let me know if this works for you! Wishing you a beautiful day
  3. Hi again TLG, Another idea, a bit heavy but quite simple. Look for all the new lines between your tags, replace them with something unique such as ##NewLine##, do your br replacements on the rest of the strings, then switch back your ##NewLine## to new lines (\r\n).
  4. Wow, beautifully intricate discussion for such a simple match, I love it! I thought it was all over in ten minutes after premiso's reply, but no! @.josh, thank you for mentioning \K. I'd read about it without really paying attention as I was on 5.2.1, but recently upgraded to 5.3.8 which supports it. Thank you for pointing it out, I'm going to start using it. Not yet supported in RegexBuddy by the way, but I found a thread from 8 months ago where Jan (the developer) says it's on the todo list. Wishing you all a fun day.
  5. If you want a regex to validate a name and address, that's a very, very tall call. But I think you know it and are asking for a first filter that only accepts certain characters. Is that right? Here is an example: [[:alnum:] .@#-]+ It allows alpha-numerical characters, spaces, periods, and the other characters between the brackets. Add any characters you like. If you want to use a backslash you will have to escape it, and possibly other special characters depending on their position in the brackets.
  6. Hi TLG, Here's one idea. The problem would be easy to solve if PCRE allowed negative lookbehinds of variable lengths. Then we could look behind the new line to see if there was a code div behind us. But PCRE doesn't allow that. You may have run into that. So, how about replacing all new lines unless they are inside a div? This may or may not suit your needs. It would certainly fix your problem for the bbc "code" tag. It would not suit your needs if you ever want to convert a new line that's inside a div... But would you want to do that? If the idea is appealing, here's an expression that replaces new lines with br tags unless the new lines are inside a div. The Match expression: (?!\r\n[^<]*?</div)\r\n The Replace expression: <br /> A sample preg: $string="First Line Second Line <div class='code'>blah blah</div"; $result = preg_replace('%(?!\r\n[^<]*?</div)\r\n%m', '<br />', $subject); echo htmlentities($result); The Input: First Line Second Line <div class='code'>blah blah</div The Output: First Line<br />Second Line<br /><div class='code'>blah blah</div So the new lines have been replaced with br tags, except inside the div. Is this a step in the right direction? Wishing you a fun day
  7. Hi MrBean, I made a simple expression to match all your urls but not www.goog (?i)\b(?:http[s]?://)?(?(?=www.)www.)(?:[-a-z\d]+\.)+[a-z]{2,4} There are a million ways to match urls, so depending on your needs, you may want to tweak it. Is this what you were looking for? Let me know if I can help further.
  8. Hi ozzy47, If I have understood, you just want to insert an image tag in the other tags, is that right? This expression does it for you. The Match expression: (<li[^>]*?><a[^>]*?>)({[^<]*?</a>) The Replace expression: \1<img src="dbtech/vbnavtabs/images/whats_new_image/whats_new.png" style="margin-bottom:.25em; vertical-align:middle;' . $image_padding . '" >\2 A preg_repace that takes the $subject string as input: $result = preg_replace('%(<li[^>]*?><a[^>]*?>)(\{[^<]*?</a>)%m', '\1<img src="dbtech/vbnavtabs/images/whats_new_image/whats_new.png" style="margin-bottom:.25em; vertical-align:middle;\' . $image_padding . \'" >\2', $subject); Is this what you were looking for? Your code boxes were pretty full so I may have misunderstood. Wishing you a fun day
  9. Hi Mcod! This regex will reverse your strings. Match expression: ([^-]*?) -(.*) Replace expression: \2 - \1 Here is an example of preg_replace integration ($subject is your string): $result = preg_replace('/([^-]*?) -(.*)/m', '\2 - \1', $subject); Is this what you were looking for?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.