mr5
mr5

Reputation: 3580

Regex: Confused about the $ operator

So I'm just messing around the internet and found this regex w/c is quite interesting. I'm very new to this stuff and I wanted to done this search pattern tonight. However, I'm quite confused in the $ operator. And I'm running out of terms what keywords should I search on Google.

Regex:

(?<=<RGBA?:(-?\d{1,3},?){3,4}>).*(?=</RGBA?>$)

Data:

<RGBA:255,255,255,2>HEY</RGBA>
<RGB:-1,-25,-3>Is this a typo?</RGB>
<RGB:255,255,255>YOH</RGB>
<RGB:0,1,2>Please let me go here :(</RGB>
<RGBA:0,255,12,255>o my, what to do here?!!!!</RGBA>
hahahah
hehehe2123
<RGB:-0,-0,-0>GET ME</RGB>

This will only get the last line GET ME, but if I remove the $ the result is as expected but fails if the string is contiguous (no newline).For example:

Data:

<RGBA:255,255,255,2>HEY</RGBA> <RGB:-1,-25,-3>Is this a typo?</RGB>

Returns:

HEY</RGBA> <RGB:-1,-25,-3>Is this a typo?

Please explain so I can resolve this.

PS. The data(string) format is for another application I made

Upvotes: 0

Views: 83

Answers (4)

Jeremy Aube
Jeremy Aube

Reputation: 616

I think what you might be looking for is something like this:

(?<=<RGBA?:(-?\d{1,3},?){3,4}>).*?(?=</RGBA?>)

The ? after the .* makes the expression lazy instead of greedy, so it won't match past the closing tag.

Upvotes: 2

Brian Stephens
Brian Stephens

Reputation: 5261

In most languages, $ matches the end of the entire input string by default. But in many languages, the meaning can be altered with an option called something like 'multi-line'. Then the ^ and $ match the beginning and end of each line, respectively.

Upvotes: 0

joelmdev
joelmdev

Reputation: 11773

$ specifies that end of the input must match the pattern. for instance, the regex

foo$

would match

myfoo

but not

foobar



^ specifies that the beginning of the input must match the pattern. So if you had the regex

^foo

it would match

foobar

but not

myfoo



if you combined both, eg

^foo$

it would match only the pattern

foo

all other input would fail.

Upvotes: 1

bstamour
bstamour

Reputation: 7766

Typically the $ operator matches the end of the input. So for example

[0-9]+$

would match one or more digits, but they must appear at the end of the string.

Edit: After following your link in the comments, the $ operator does match the end of the input string, which explains why when you keep it in, you only get the last match.

Upvotes: 0

Related Questions