Jump to content

Archived

This topic is now archived and is closed to further replies.

HuggieBear

Check first character matches the last character in a string

Recommended Posts

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 line
2. Capture the first character, doesn't matter what it is
3. Then there has to be another character, again, doesn't matter what, but has to be atleast one
4. Then an identical character to the first
5. Then the end of the line

Regards
Huggie

Share this post


Link to post
Share on other sites
[tt]1 [/tt]is the literal number "1". You want the first backreference, which is[tt] \1[/tt].

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
That's perfect, I'm only matching on a single line and I certianly don't want to use the /s modifier.

Regards
Huggie

Share this post


Link to post
Share on other sites

×

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.