rangers8905
rangers8905

Reputation: 37

Regular Expression that matches text between a specific string and a character

I have a problem that I can't seem to find an answer to online. I have some text:

Variable1 = Value1, Variable2 = Value2, Variable3 = Value3, Variable4 = Value4,

Lets say the string I want to find is "Variable2 = "

I want "Value2" returned. It can't include the comma.

So far the best I could come up with is:

(Variable2)\s\=\s(.+?),

But this returns Variable2 = Value2,

Additional Info

I'm stuck using a program called Automate BPA. It has some built in support for regex.

I hope it's possible and I appreciate any help!

Upvotes: 0

Views: 87

Answers (2)

Bohemian
Bohemian

Reputation: 424973

Use a look behind and a look ahead around your target:

(?<=Variable2\s?=\s?).+?(?=,|$)

The look ahead matches on comma or end of input, so it will still match the last term if you search for that.

Upvotes: 1

Jonnny
Jonnny

Reputation: 5039

$var = 'Variable1 = Value1, Variable2 = Value2, Variable3 = Value3, Variable4 = Value4,';

$new = preg_split( '/,/', $var);

print_r($new);

That will break it up into an indexed array

Array ( [0] => Variable1 = Value1 [1] => Variable2 = Value2 [2] => Variable3 = Value3 [3] => Variable4 = Value4 [4] => )

Upvotes: 0

Related Questions