Best way to extract data from string

user3033514

I have a string:

__cfduid=d2eec71493b48565be764ad44a52a7b191399561601015; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.planetminecraft.com; HttpOnly

I want to use regex and get something like this:

[0] = __cfduid=d2eec71493b48565be764ad44a52a7b191399561601015
[1] = expires=Mon, 23-Dec-2019 23:50:00 GMT
[2] = path=/
[3] = domain=.planetminecraft.com
[4] = HttpOnly

I tried this regex:

[\A|;](.*?)[\Z|;]

I don't understand why \A. works but [\A] not, how can I create (\A or ;)?

In final form of this regex I want to get from string this:

[0] = {
    [0] = __cfduid
    [1] = d2eec71493b48565be764ad44a52a7b191399561601015
}
[1] = {
    [0] = expires
    [1] = Mon, 23-Dec-2019 23:50:00 GMT
}
[2] = {
    [0] = path
    [1] = /
}
[3] = {
    [0] = domain
    [1] = .planetminecraft.com
}
[4] = {
    [0] = HttpOnly
}
Jerry

You can try matching on this regex:

\s*([^=;]+)(?:=([^=;]+))?

Description:

\s*         # Match any spaces
([^=;]+)    # Match any non = or ; characters
(?:
  =         # Match an = sign
  ([^=;]+)  # Match any non = or ; characters.
)?          # Make this group optional

regex101 demo

In code:

string text = "__cfduid=d2eec71493b48565be764ad44a52a7b191399561601015; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.planetminecraft.com; HttpOnly";

var regex = new Regex(@"\s*([^=;]+)(?:=([^=;]+))?");
var matches = regex.Matches(text);
foreach (Match match in matches)
{
    Console.WriteLine(match.Groups[1].Value + "\n" + match.Groups[2].Value + "\n");
}

ideone demo


\A works but [\A] does not because when you put \A in a character class, it loses its meaning like most regex metacharacters. For instance, + and * also lose their meaning. In [\A], the regex is actually trying to match \A and since it doesn't have a particular meaning in a character class, it means a literal A.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Best way to extract parts from a string

From Dev

MySQL - Best way to extract integer from string

From Dev

Java Best way to extract parts from a string

From Dev

Best way to extract data from dict with multiple dicts and lists?

From Dev

Best way to extract and use data from a server database?

From Dev

Extract data from string

From Dev

Extract data from string

From Dev

Best way to extract code from controller

From Dev

Best way to extract code from controller

From Dev

Extract the best attributes from a data.table

From Dev

Extract data from Json string

From Dev

extract data in mysql from the string

From Dev

Extract specific data from a string

From Dev

Extract data from string with regex

From Dev

extract data from JSON string

From Dev

Extract Name from a Data String

From Dev

Extract data format from string

From Dev

Extract data from xml string

From Dev

What is the best way to extract data within a date range?

From Dev

Best way to grab a specific position from a string

From Java

Best way to strip punctuation from a string

From Dev

Best way to grab a specific position from a string

From Java

Is there a better way to extract information from a string?

From Dev

pythonic way to extract string from this list

From Dev

Fast way to extract tokens from scala string

From Dev

Most Pythonic way to extract a number from a string

From Dev

Best way to pass single data string from an action to a component React/Redux/ES6

From Dev

Best way to extract a reference category from a glm model?

From Dev

Best way to identify and extract dates from text Python?