0Neji
0Neji

Reputation: 1128

Regex - Match Word Aslong As Nothing Follows It

Having a little trouble with regex. I'm trying to test for a match but only if nothing follows it. So in the below example if I go to test/create/1/2 - it still matches. I only want to match if it's explicitally test/create/1 (but the one is dynamic).

if(preg_match('^test/create/(.*)^', 'test/create/1')):
    // do something...
endif;

I've found some answers that suggest using $ before my delimiter but it doesn't appear to do anything. Or a combination of ^ and $ but I can't quite figure it out. Regex confuses the hell out of me!

EDIT:

I didn't really explain this well enough so just to clarify:

I need the if statement to return true if a URL is test/create/{id} - the {id} being dynamic (and of any length). If the {id} is followed by a forward slash the if statement should fail. So that if someone types in test/create/1/2 - it will fail because of the forward slash after the 1.

Solution

I went for thedarkwinter's answer in the end as it's what worked best for me, although other answers did work as well.

I also had to add an little extra in the regex to make sure that it would work with hyphens as well so the final code looked like this:

if(preg_match('^test/create/[\w-]*$^', 'test/create/1')):
        // do something...
endif;

Upvotes: 1

Views: 7247

Answers (5)

Neil Neyman
Neil Neyman

Reputation: 2158

The ^ is an anchor for the beginning of the line, i.e. no characters occurring before the ^ . Use a $ to designate the end of the string, or end of the line.

EDIT: wanted to add a suggestion as well:

Your solution is fine and works, but in terms of style I'd advise against using the carat (^) as a delimiter -- especially because it has special meaning as either negation or as a start of line anchor so it's a bit confusing to read it that way. You can legally use most special characters as long as they don't occur (or are escaped) in the regex itself. Just talking about a matter of style/maintainability here.

Of course nearly every potential delimiter has some special meaning, but you also often tend to see the ^ at the beginning of a regex so I might chose another alternative. For example # is a good choice here :

if(preg_match('#test/create/[\w-]*$#', $mystring)) {
 //etc
}

Upvotes: 1

falsetru
falsetru

Reputation: 369474

Use following regular expression (use $ to denote end of the input):

'|test/create/[^/]+$|'

If you want only match digits, use folloiwng instead (\d match digit character):

'^test/create/\d+$^'

Upvotes: 3

thedarkwinter
thedarkwinter

Reputation: 96

/w matches word characters, and $ matches end of string

if(preg_match('^test/create/\w*$^', 'test/create/1'))

will match test/create/[word/num] and nothing following.

I think thats what you are after.

edit added * in \w*

Upvotes: 4

Alec Teal
Alec Teal

Reputation: 5938

Here you go:

"/^test\\/create\\/([^\\/]*)$/"

This says: The string that starts with "test" followed by a forward slash (remember the first backslash escapes the second so PHP puts a letter backslash in the input, which escapes the / to regex) followed by create followed by a forward slash followed by and capture everything that isn't a slash which is then the end of the string.

Comment if you need more detail

I prefer my expressions to always start with / because it has no meaning as a regex character, I've seen @ used, I believe some other answer uses ^, this means "start of string" so I wouldn't use it as my regex delimiters.

Upvotes: 3

knittl
knittl

Reputation: 265956

The regex abc$ will match abc only when it's the last string.

abcd # no match
dabc # match
abc # match

Upvotes: 0

Related Questions