R01010010
R01010010

Reputation: 5958

Split matches in regex

Having this string:

Paquete Trend Exterior -alfombrillas, faros antiniebla, llantas de aleación de 15- (300), Paquete Winter -asientos delanteros calefactables, parabrisas térmico- (300), Paquete City -control de aparcamiento trasero, retrovisores plegables eléctricamente- (250).

I tried this reg expression:

 /.\*-.\*- \\(.*\\)/

To get this:

Array
(
    [0] => Array
        (
            [0] => Paquete Trend Exterior -alfombrillas, faros antiniebla, llantas de aleación de 15- (300)
            [1] => Paquete Winter -asientos delanteros calefactables, parabrisas térmico- (300)
            [2] => Paquete City -control de aparcamiento trasero, retrovisores plegables eléctricamente- (250)
        )    
)

But i'm having this:

Array
(
    [0] => Array
        (
            [0] => Paquete Trend Exterior -alfombrillas, faros antiniebla, llantas de aleación de 15- (300), Paquete Winter -asientos delanteros calefactables, parabrisas térmico- (300), Paquete City -control de aparcamiento trasero, retrovisores plegables eléctricamente- (250)
        )
)

How can i split the results?


EXTRA

String given could be like this too:

Bola de remolque desmontable (350 euros), control de velocidad -mega, chachi- (150), cristales traseros tintados (120), pintura blanco sólido (150), Paquete Titanium Cuero -llantas de aleación de 17, tapicería de cuero, asientos delanteros calefactables- (600), Paquete Panorámico -techo solar panorámico, cristales tintados- (600).

And id like to match the "Bola de remolque desmontable (350 euros)" format also. Is it possible?

Upvotes: 0

Views: 94

Answers (5)

sp00m
sp00m

Reputation: 48807

Try to split by (?<=[(]\d+( euros)?[)]), +.

Upvotes: 1

Bernhard Barker
Bernhard Barker

Reputation: 55589

The problem is the the * is greedy, thus it will match as much of the string as possible. You can just make the * non-greedy, as in *?.

Thus:

/.*?-.*?- \(.*?\)/

Prints:

[0] => Paquete Trend Exterior -alfombrillas, faros antiniebla, llantas de aleación de 15- (300)
[1] => , Paquete Winter -asientos delanteros calefactables, parabrisas térmico- (300)
[2] => , Paquete City -control de aparcamiento trasero, retrovisores plegables eléctricamente- (250)

If you want the , to be removed as well, consider making the first character \w, if this is generic enough ([^, ] instead of \w will also work).

/\w.*?-.*?- \(.*?\)/

Prints:

[0] => Paquete Trend Exterior -alfombrillas, faros antiniebla, llantas de aleación de 15- (300)
[1] => Paquete Winter -asientos delanteros calefactables, parabrisas térmico- (300)
[2] => Paquete City -control de aparcamiento trasero, retrovisores plegables eléctricamente- (250)

For the second string

You could make -.*?- optional by surrounding it in brackets and putting a ? after it (the ?: just makes it a non-capturing group, so it doesn't output that as well, it doesn't change the matching).

/\w.*?(?:-.*?-)? \(.*?\)/

Or you could do something simpler which would work for both strings:

/\w.*?\)/

Upvotes: 3

user23031988
user23031988

Reputation: 330

Here -> http://regexr.com?355dp

RegExp: /[^, ][^(]*[(][0-9]*[)]/g pattern: [^, ][^(]*[(][0-9]*[)]

Upvotes: 0

Kendall Frey
Kendall Frey

Reputation: 44316

Change your regex to:

/.*?-.*?- \(.*?\)/

*? matches zero or more times, but unlike *, it matches as few times as possible.

In your version, the first .* matches everything up to Paquete City.

Read http://www.regular-expressions.info/repeat.html for more information.

You could also use:

/[^-]*-[^-]*- \([^\)]*\)/

It's usually a good idea to be more specific about what can match, rather than using ..

Upvotes: 1

gitaarik
gitaarik

Reputation: 46280

What do you think of this one?

/.*?\(\d+\)[,\.]/

http://www.debuggex.com/r/ArLYC3VNceye7z9A

Upvotes: 1

Related Questions