Click here to Skip to main content
15,566,496 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For example if I have address is xyz here the word address is static word but xyz will be dynamically changing in all files could anyone help how to code this?

What I have tried:

For example if I have address is xyz here the word address is static word but xyz will be dynamically changing in all files could anyone help how to code this?
Posted
Updated 7-Sep-20 21:19pm
Comments
Maciej Los 8-Sep-20 2:14am    
"the word address is static word but xyz will be dynamically changing" - ?!?
What do you mean? What is "xyz"?

1 solution

You will need to be a lot more specific with your rules: it's very feasible to "pull" an address (or any other element) out of text but you need to be specific about what prefix and suffix delimits it.

With only the example above, you could say 'before the address will be the words "address is " and it will end with " here"' - and then it's relatively simple to build a Regular Expression that extracts "xyz" (or whatever that is in the "real world"):
(?<=address is ).*?(?= here)

Then it's easy to get the address with C# code using that Regex:
C#
using System.Text.RegularExpressions;
...
        public static Regex regex = new Regex("(?<=address is ).*?(?= here)", RegexOptions.CultureInvariant | RegexOptions.Compiled);
        private void MyButton_Click(object sender, EventArgs e)
            {
            string inputText = "For example if I have address is xyz here the word address is static word but xyz will be dynamically changing in all files could anyone help how to code this?";
            Match m = regex.Match(inputText);
            if (m.Success)
                {
                string address = m.Value;
                }
            }

But ... you would need to look closely at real, live, data and decide what prefix and suffixes you actually need.

This may help:Expresso[^] - it's free, and it examines, tests, and generates Regular expressions.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900