Answered by:
how to extract words from the list using regex?

Question
-
User-1404113929 posted
hi,
I have list of string values like
helloworld aaa
hello asdasd
hellotest adfasasd
test hello adasd
helloworld adasd
when i search for hello in that list i want result like
helloworld
hello
hellotest
hello
helloworld.
Thanks,
Murali
Monday, August 29, 2016 8:40 AM
Answers
-
User283571144 posted
Hi balu.devara,
how to extract words from the list using regex?According to your description, I suggest you could use String.Split method split the string, firstly.
Then you could use Regex.IsMatch method to get the result.
Regax:
^[a-zA-Z]*hello[a-zA-Z]*$
Pattern
Description
^
Begin the match at the beginning of the line.
[a-zA-Z]
Match a single alphabetic character (a through z or A through Z)
*
Match 0-MAX characters.
hello
Match hello
[a-zA-Z]
Match a single alphabetic character (a through z or A through Z)
*
Match 0-MAX characters.
$ End the match at the end of the line
More details, you could refer to follow codes and link:
static void Main(string[] args) { List<string> l1 = new List<string>(); List<string> result = new List<string>(); l1.Add("helloworld aaa"); l1.Add("hello asdasd"); l1.Add("hellotest adfasasd"); l1.Add("test hello adasd"); l1.Add("helloworld adasd"); Regex rgx = new Regex(@"^[a-zA-Z]*hello[a-zA-Z]*$"); foreach (string item in l1) { string[] temp = item.Split(' '); for (int i = 0; i < temp.Length; i++) { if (rgx.IsMatch(temp[i])) { result.Add(temp[i]); } } } foreach (string item in result) { Console.WriteLine(item); } Console.Read(); }
Regex.IsMatch:
https://msdn.microsoft.com/en-us/library/3y21t6y4(v=vs.110).aspx
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 29, 2016 12:36 PM -
User-1404113929 posted
hi,
i used bellow code , it works fine.
var searchKeysList = new Collection<string>(); var cacheKey = Common.Constants.CacheKeys.ActivitySearchKeys; Collection<string> resultKeysList = new Collection<string>(); var regx = new System.Text.RegularExpressions.Regex(string.Format(@"(?i)\b\w*{0}\w*", searchTerm)); if (!this.CacheService.IsExists(cacheKey)) { searchKeysList = this.ActivityDAO.GetSearchKeys(); this.CacheService.AddDataToCache(cacheKey, searchKeysList); } else { searchKeysList = this.CacheService.GetDataFromCache<Collection<string>>(cacheKey); } var filterKeysList = searchKeysList.ToList().Where(x => x.IndexOf(searchTerm, StringComparison.InvariantCultureIgnoreCase) >= 0).ToList(); foreach (var filterKey in filterKeysList) { foreach (Match m in regx.Matches(filterKey)) { resultKeysList.Add(m.Value); } } resultKeysList = resultKeysList.GroupBy(x => x).Select(g => g.Key).OrderBy(x => x).ToCollection();
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 30, 2016 10:22 AM
All replies
-
User283571144 posted
Hi balu.devara,
how to extract words from the list using regex?According to your description, I suggest you could use String.Split method split the string, firstly.
Then you could use Regex.IsMatch method to get the result.
Regax:
^[a-zA-Z]*hello[a-zA-Z]*$
Pattern
Description
^
Begin the match at the beginning of the line.
[a-zA-Z]
Match a single alphabetic character (a through z or A through Z)
*
Match 0-MAX characters.
hello
Match hello
[a-zA-Z]
Match a single alphabetic character (a through z or A through Z)
*
Match 0-MAX characters.
$ End the match at the end of the line
More details, you could refer to follow codes and link:
static void Main(string[] args) { List<string> l1 = new List<string>(); List<string> result = new List<string>(); l1.Add("helloworld aaa"); l1.Add("hello asdasd"); l1.Add("hellotest adfasasd"); l1.Add("test hello adasd"); l1.Add("helloworld adasd"); Regex rgx = new Regex(@"^[a-zA-Z]*hello[a-zA-Z]*$"); foreach (string item in l1) { string[] temp = item.Split(' '); for (int i = 0; i < temp.Length; i++) { if (rgx.IsMatch(temp[i])) { result.Add(temp[i]); } } } foreach (string item in result) { Console.WriteLine(item); } Console.Read(); }
Regex.IsMatch:
https://msdn.microsoft.com/en-us/library/3y21t6y4(v=vs.110).aspx
Best Regards,
Brando
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 29, 2016 12:36 PM -
User-1404113929 posted
hi,
thanks for your replay , i found another way to solve the issue.
thanks,
murali
Tuesday, August 30, 2016 10:17 AM -
User-1404113929 posted
hi,
i used bellow code , it works fine.
var searchKeysList = new Collection<string>(); var cacheKey = Common.Constants.CacheKeys.ActivitySearchKeys; Collection<string> resultKeysList = new Collection<string>(); var regx = new System.Text.RegularExpressions.Regex(string.Format(@"(?i)\b\w*{0}\w*", searchTerm)); if (!this.CacheService.IsExists(cacheKey)) { searchKeysList = this.ActivityDAO.GetSearchKeys(); this.CacheService.AddDataToCache(cacheKey, searchKeysList); } else { searchKeysList = this.CacheService.GetDataFromCache<Collection<string>>(cacheKey); } var filterKeysList = searchKeysList.ToList().Where(x => x.IndexOf(searchTerm, StringComparison.InvariantCultureIgnoreCase) >= 0).ToList(); foreach (var filterKey in filterKeysList) { foreach (Match m in regx.Matches(filterKey)) { resultKeysList.Add(m.Value); } } resultKeysList = resultKeysList.GroupBy(x => x).Select(g => g.Key).OrderBy(x => x).ToCollection();
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, August 30, 2016 10:22 AM