Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx to match full string

Tags:

regex

I am trying to create a regex for 301s that will help me identify the url: site.com/abc/ and redirect to site.com/xyz/. I've tried regex as ^abc/? and it works fine but the problem is even urls like site.com/123/sdas/abc/213 are getting caught. How can I ensure only /abc gets matched with the full string url?

like image 803
xoail Avatar asked May 13 '11 20:05

xoail


People also ask

How do I match an entire line in regex?

To expand the regex to match a complete line, add ‹ . * › at both ends. The dot-asterisk sequences match zero or more characters within the current line.

What is the regex for a string?

A regular expression (regex) defines a search pattern for strings. The search pattern can be anything from a simple character, a fixed string or a complex expression containing special characters describing the pattern.

Which modifier is used for matching a regex in the complete string?

The "m" modifier specifies a multiline match. It only affects the behavior of start ^ and end $. ^ specifies a match at the start of a string.


2 Answers

Use the end of line anchor $:

^abc/$

This ensures that the exact string abc/ will be matched.

like image 181
Oded Avatar answered Sep 28 '22 09:09

Oded


$ matches end of string:

^abc/?$
like image 22
manojlds Avatar answered Sep 28 '22 09:09

manojlds