HuggieBear Posted September 15, 2006 Share Posted September 15, 2006 Morning all,I have the following expression which I think works well. What I want in short is a string to start and end with the same character, with atleast one thing in between.[code=php:0]/^(.).+\1$/[/code]Below is the details of what I think the above does, but wondered if there's a better way to do it...1. Start at the beginning of the line2. Capture the first character, doesn't matter what it is3. Then there has to be another character, again, doesn't matter what, but has to be atleast one4. Then an identical character to the first5. Then the end of the lineRegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/20840-check-first-character-matches-the-last-character-in-a-string/ Share on other sites More sharing options...
effigy Posted September 15, 2006 Share Posted September 15, 2006 [tt]1 [/tt]is the literal number "1". You want the first backreference, which is[tt] \1[/tt]. Quote Link to comment https://forums.phpfreaks.com/topic/20840-check-first-character-matches-the-last-character-in-a-string/#findComment-92407 Share on other sites More sharing options...
HuggieBear Posted September 15, 2006 Author Share Posted September 15, 2006 Effigy, that's what I had, I copied and pasted it direct from the script.It's an issue with putting the [ php ] [ /php ] tags around it, they stripped it out.Huggie Quote Link to comment https://forums.phpfreaks.com/topic/20840-check-first-character-matches-the-last-character-in-a-string/#findComment-92461 Share on other sites More sharing options...
effigy Posted September 15, 2006 Share Posted September 15, 2006 Ahh. Then the expression you have looks good. Just keep in mind that[tt] . [/tt] matches everything but a new line without the[tt] /s [/tt] modifier, and that your pattern will only match one line without the[tt] /m [/tt] modifier. Quote Link to comment https://forums.phpfreaks.com/topic/20840-check-first-character-matches-the-last-character-in-a-string/#findComment-92466 Share on other sites More sharing options...
HuggieBear Posted September 15, 2006 Author Share Posted September 15, 2006 That's perfect, I'm only matching on a single line and I certianly don't want to use the /s modifier.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/20840-check-first-character-matches-the-last-character-in-a-string/#findComment-92471 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.