question

zequion-6374 avatar image
0 Votes"
zequion-6374 asked zequion-6374 edited

Regex Contains in mode Like

I want to be able to find any of the indicated words in Like and Not Like mode, ex.:"word" / ".word." but that does not take into account the initial and final space. Space is allowed in compound word. Ex: "word1 word2".

I use System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase and I would like to know how to include that does not differentiate letters with accents from letters without accents.

My Regex: "\b(?:.road.|.house.|.street.)\b"
Text: "have one house free."
It should return: Match: 9 length 5 End 13
You can try it at: https://regex101.com

Note:If I try "\b(?:road|house|street)\b" returns 9-14 (bad, is 9-13). Not Like Mode.
Note:If I try "\b(?:.road.|.house.|.street.)\b" returns 8-15 (bad, is 9-13). Like Mode.
Note:If I try "\b(?:.road.|.ous.|.street.)\b" returns 9-14 (bad, is 9-13). Like Mode.





dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


The expression "(?:road|house|street)" seems to work in this case. Show an example that needs a different approach.

0 Votes 0 ·
zequion-6374 avatar image
0 Votes"
zequion-6374 answered

But I need it to be Like mode with points ".words."

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered HuiLiu-MSFT commented

I am afraid the link you provided is invalid. After tested on my side, using "\b(?:.road.|.house.|.street.)\b" in the following code matches correctly.
You could refer to the following code to use regular expressions to match any word that contains a specific letter. If I misunderstood, please let me know.

Details:
(?i) -Case insensitive mode
\b-a Leading word boundary
(?=\w*m)-After 0+ word characters (ie letters, numbers or underscores), there must be m
(?=\w*a)-After 0+ characters, there must be a
(?=\w*h)-0+ After the word character, there must be h
(?=\w*d)-0+ character, there must be d
\w+-1 or more letters, numbers or underscores (you can replace with \p{L} to match only letters).

The code:

 using System;
 using System.Linq;
 using System.Text.RegularExpressions;
 namespace ConsoleApp1
 {
   class Program
   {
     static void Main(string[] args)
     {
       var str = "have one house free in the road.";
       var pattern = @"\b(?:.road.|.house.|.street.) \b";
       Regex regex = new Regex(pattern);
       Match match = regex.Match(str);
       if (match.Success)
       {
         Console.WriteLine(match.Value);
       }
       var letters = "ouse";    //output house
      //var letters = "oad";    // Output road
       var pat = string.Format(@"\b{0}\w+\b", string.Join("", letters.Select(s => string.Format(@"(?=\w*{0})", s))));
       var result = Regex.Matches(str, pat, RegexOptions.IgnoreCase)
           .Cast<Match>()
           .Select(match => match.Value)
           .ToList();
       Console.WriteLine(String.Join("\n", result));
     }
   }
 }

The picture of result:
132283-v.png


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


v.png (2.5 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

it is sought that in both cases it returns "house" without spaces position 9-13

0 Votes 0 ·
HuiLiu-MSFT avatar image HuiLiu-MSFT zequion-6374 ·

Hi,@zequion-6374. What is the relevant code for your output 9-14 (9 End 13)? Could you show me your complete code for analysis and testing?

0 Votes 0 ·
zequion-6374 avatar image
0 Votes"
zequion-6374 answered HuiLiu-MSFT commented

I use https://regex101.com for test.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@zequion-6374. There may be a problem with the website's counting method. I used the following examples for testing and both have the same problem. Even if the matched word is at the end of the sentence and there is no space after it.
\bm\S*e\b or m\S*e Test String: make maze and manage to measure
132937-q.png
\bS\S* Test String: A Thousand Splendid Suns
132938-s.png
\b[M]\w+ or [M]\w+ Test String: Mahesh Chand, Raj Kumar, Mike Gold, Allen O'Neill, Marshal Troll
133003-e.png

0 Votes 0 ·
q.png (8.3 KiB)
s.png (5.3 KiB)
e.png (4.7 KiB)
zequion-6374 avatar image
0 Votes"
zequion-6374 answered zequion-6374 edited

I do not doubt what you say but someone is doing things wrong. I have tested the Regular Expression and it matches the return from the web. It returns the position of the previous space and the posterior space when I use "\b(?:.road.|.house.|.street.)\b". I think the ^\s parameter applies to the text but not to the results.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.