A
A
Anton Perevoshchikov2015-12-16 13:25:39
PHP
Anton Perevoshchikov, 2015-12-16 13:25:39

How to select a string up to certain characters?

For example, you need to select a string up to one of the following characters: ,;.
In this case, the solution is simple: /^[^,;]+/
Actually, the question is: how to select a string up to a group of characters?
For example, before \s[a-z](space and any letter). Something like: /^[^,;ГРУППА]+/
UPD: you need to select a string, both up to individual characters, and up to some group of characters

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
MiiNiPaa, 2015-12-16
@Fett

Use capturing groups: https://regex101.com/r/qN5qP8/3
UPD : Updated, regexp now with alternative groups

A
alexxandr, 2015-12-16
@alexxandr

(.+?)GROUP

I
Igor Deyashkin, 2015-12-16
@Lobotomist

The desired sequence must be grouped using brackets. The (?=) construct means that whatever is in the brackets will not be part of the match.
You can do this:
But in this case, the part of the string you are interested in will be in the first group, and the full match will be along with a space and a letter at the end.
I recommend two services in which it is convenient to check regular expressions:
* pcre.ru ( example for your case)
* regex101.com

M
Mikhail Osher, 2015-12-16
@miraage

Positive lookahead .

$re = '(.+?)(?=[,;]+)'; // второе условие модифицируйте и забирайте данные

I
Ivanq, 2015-12-17
@Ivanq

/^.*(?=[,;])/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question