Are regular expressions possible?

I am trying to split a string into tokens (via regular expressions) as follows:

Example # 1
input line: 'hello'
first token: '
second token: hello
third token:'

Example # 2
input line: 'hello world'
first token: '
second token: hello world
third token:'

Example # 3
input line: hello world
first token: hello
second token:world

ie, separate the string only if it is NOT in single quotes, and single quotes should be in their own token.

This is what I have so far:

string pattern = @"'|\s";
Regex RE = new Regex(pattern);
string[] tokens = RE.Split("'hello world'");

This will work, for example, # 1 and example # 3, but it will NOT work, for example, # 2. I am wondering if it is theoretically possible to achieve what I want with regular expressions

+3
8

' , , RegExp . , , . , ((\w+)+\b) . , , .

( ). , .

+1

, . , . , - - .

+5

.

+3

'[^']+' . , (')([^']+)('). , . , .

:. , , , , . , , - .

+2

, .

foreach (String s in Regex.Split(input, @"('[^']+')")) {
    // Check first if s is a quote.
    // If so, split out the quotes.
    // If not, do what you intend to do.
}

(: , , Regex.Split )

+1

, , :

(?<quot>')?(?<words>(?(quot)[^']|\w)+)(?(quot)')

, , . . "quot" "words".

+1

Split , MatchCollection :

string str = "hello world, 'HELLO WORLD': we'll be fine.";
MatchCollection matches = Regex.Matches(str, @"(')([^']+)(')|(\w+)");

. , .
-.net Match s. Group - ('hello world'), (', hello world, '). , .
. LINQ:

var tokens = from match in matches.Cast<Match>()
             from g in match.Groups.Cast<Group>().Skip(1)
             where g.Success
             select g.Value;

tokens :
hello, world, ', hello world, ', we, ll, be, fine

+1

:

([']*)([a-z]+)([']*)

This finds 1 or more single quotes at the beginning and end of the line. Then it finds 1 or more characters in the az set (if you don't set it case insensitive, it will find only lowercase characters). He groups them so that group 1 has, "group 2 (or more) has words that break down into everything that is not the symbol az, and the last group has a single quote, if one exists.

0
source

Source: https://habr.com/ru/post/1732094/


All Articles