p.magalhaes
p.magalhaes

Reputation: 8374

Regular expression to validate and parse

I want to make a regular expression to validate and parse the string below:

TESTE_CASE.PESSOAS;CO_CNPJ_CEI BETWEEN (0000000000370000000000, 0000000000370000009999);*

Using the following regular expression I can validate the string:

\s*\w+.\w+\s*;\s*\w+\s+(?i)BETWEEN\s+\(\s*\w+\s*,\s*\w+\s*\)\s*;\s*\*\s*

What is the best way to parse this string? I need output like below:

String1 = TESTE_CASE.PESSOAS
String2 = CO_CNPJ_CEI
String3 = 0000000000370000000000
String4 = 0000000000370000009999
String5 = *

Thanks,

Upvotes: 0

Views: 102

Answers (3)

Ria
Ria

Reputation: 10347

You can enclose your needed expressions by ():

( subexpression )
Captures the matched subexpression and assigns it a zero-based ordinal number.

and get captured expression by index

or use named groups:

(?< name > subexpression)
Captures the matched subexpression into a named group.

and get captured expression by name

Upvotes: 1

godspeedlee
godspeedlee

Reputation: 672

try with this: (\b(?!BETWEEN\b)[\w.]+\b|\*)

Upvotes: 0

John Corbett
John Corbett

Reputation: 1615

I would do it like this

\s*([^;]+);([^\s]+).*?\(\s*(\d+)\s*,\s*(\d+).*?;(\*)?

$1 = TESTE_CASE.PESSOAS
$2 = CO_CNPJ_CEI
$3 = 0000000000370000000000
$4 = 0000000000370000009999
$5 = *

Upvotes: 1

Related Questions