Ali.Turkkan
Ali.Turkkan

Reputation: 265

How to find and get a string data

I have a script. I get this script as a string through the database. I need a specific value in this script. How can I get this data out of string?

The data(type=str) I extracted from the database

data = """if(description)
{
  script_name("name");
  script_summary("summary");
  script_version("version");
  script_tag(name:"base_vector", value:"AV:N/AC:L/Au:N/C:C/I:C/A:C");
  exit(0);
}"""

The data I want for each script

base_vector: AV:N/AC:L/Au:N/C:C/I:C/A:C

This value is changing for each script.

Upvotes: 3

Views: 120

Answers (2)

Mete Han Kahraman
Mete Han Kahraman

Reputation: 760

here is half of it leaving rest to you

m = re.search('(?<=value:")..[A-Z|a-z|:|/]*', data)
print(m.group(0))

'AV:N/AC:L/Au:N/C:C/I:C/A:C'

Read this whole document at some point of your life: https://docs.python.org/3/library/re.html

Upvotes: 0

grapes
grapes

Reputation: 8646

Consider using regular expressions for that.

import re

rx = r'name\s*\:\s*\"(\w+)\"\s*\,\s*value\s*\:\s*\"(.*)\"'

text = """if(description)
{
  script_name("name");
  script_summary("summary");
  script_version("version");
  script_tag(name:"base_vector", value:"AV:N/AC:L/Au:N/C:C/I:C/A:C");
  exit(0);
}"""

m = re.search(rx, text)
print(m.group(1) + ': ' + m.group(2))

The output is:

base_vector: AV:N/AC:L/Au:N/C:C/I:C/A:C

You can enhance regular expression, but provided one will work on your input data.

Note lots of \s* tokens in expression - they allow using spaces in input data.

Upvotes: 3

Related Questions