Regex Needed Query

Hi Team My Input Is
Vendor TaxID-NV055534,Name-Nvidia,Address-Santa Clara,City-California,Country-USA
I want Output after -
For every word I need Regex query Please help

Hi,

Can you try the following expression?

yourString ="Vendor TaxID-NV055534,Name-Nvidia,Address-Santa Clara,City-California,Country-USA"

 dict = yourString.Split({","c}).ToDictionary(Function(s) s.Split({"-"c})(0),Function(s) s.Split({"-"c})(1))

Then

dict("Vendor TaxID")
dict("Name")

etc.

Note: dict is Dictionary<string,string> type
Regards,

1 Like

Hi @Kuldeep_Pandey

Use the following Regex expression:

Vendor TaxID:

(?<=Vendor TaxID\-)\w+


Name:

(?<=Name\-)[A-Za-z\s]+


Address:

(?<=Address\-)[A-Za-z\s]+


City:

(?<=City\-)[A-Za-z\s]+


Country:

(?<=Country\-)[A-Za-z\s]+

Hope it helps!!
Regards,

If you want all the values in one go then try below
(?<=\-).*?(?=\,)


If you want one by one then try below
(?<=TaxID-).*?(?=\,)


Change the keyword according to your need.

Your Address pattern will not work if address is of more than or less than 2 words. Same case go for City as well (except if it is of more than 1 word)

Hi @AkshaySandhu

Below regex would work for any number of words:

Address:

(?<=Address\-)[A-Za-z\s]+

City:

(?<=City\-)[A-Za-z\s]+

Not Able to Try can you provide xml or SS Of Code

Can you explain me both

Here you are.Can you try this?

Main.xaml (6.1 KB)

Hi @Kuldeep_Pandey

(?<=TaxID-)([A-Z0-9]+)(?=,)
(?<=Name-)([A-Za-z]+)(?=,)
(?<=Address-)([A-Za-z\s]+)(?=,)
(?<=City-)([A-Za-z\s]+)(?=,)
(?<=Country-)([A-Za-z\s]+)

Hope it helps!!

If sequence of your required data is fixed i.e. TaxID will always be first, followed by Name, Address etc. then you can try below:

str_TaxID = System.Text.RegularExpressions.Regex.Matches(str_Input, "(?<=\-).*?(?=\,)")(0).Value
str_Name = System.Text.RegularExpressions.Regex.Matches(str_Input, "(?<=\-).*?(?=\,)")(1).Value

etc.

In 2nd option sequence of data does not matter. It will work even if TaxID is at the last position.
Name-Nvidia,Address-Santa,City-New York,Country-USA,TaxID-NV055534

str_TaxID = System.Text.RegularExpressions.Regex.Matche(str_Input, "(?<=TaxID-).*?(?=\,)").Value

I hope this helps.

Its Working can you explain me
yourString.Split({",“c}).ToDictionary(Function(s) s.Split({”-“c})(0),Function(s) s.Split({”-"c})(1))

Hi,

yourString.Split({",“c}) returns array of string : { “Vendor TaxID-NV055534” , “Name-Nvidia” , “Address-Santa Clara” , “City-California” , “Country-USA” }

Next, ToDictionary is LINQ method to create Dictionary. In this case, key is s.Split({"-"c})(0) (e.g. Vendor TaxID) and Value is s.Split({"-"c})(1) (e.g. NV055534)

Regards,

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.