Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: match pattern as long as it's not in the beginning

Assume the following strings:

aaa bbb ccc bbb aaa ccc 

I want to match aaa as long as it is not at the start of the string. I'm trying to negate it by doing something like this:

[^^]aaa 

But I don't think this is right. Using preg_replace.

like image 208
StackOverflowNewbie Avatar asked Mar 27 '13 21:03

StackOverflowNewbie


People also ask

How do you search for a regex pattern at the beginning of a string?

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.

How do I specify start and end in regex?

To match the start or the end of a line, we use the following anchors: Caret (^) matches the position before the first character in the string. Dollar ($) matches the position right after the last character in the string.

What is the regex pattern for end of string?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.


1 Answers

You can use a look behind to make sure it is not at the beginning. (?<!^)aaa

like image 195
Joe Avatar answered Oct 16 '22 18:10

Joe