How to get 2 words in both sides of some word

Hello everyone, I want to take “I dance” from the “Do you know that I can dance ?” using regex.
Please help.

Thanks!

@Temuulen_Buyangerel

[a-z]+(?=\s+?)

Ragards

@Temuulen_Buyangerel

(I\s+dance)
Hope it helps

@Temuulen_Buyangerel - Can you give more (at least 3) examples of other sentences and your expected outcome? Having several examples make the RegEx creation a whole lot easier. With only one example, you can make a RegEx that works but won’t work on other situations.

Thanks!

1 Like

Your can try this ,

(?<=\s*that) . (?=\s can) -----this can fetch you “I”
(?<=\s can). *(?=\s?) ---------this can fetch you “dance”

then you can put a log message and concatenate both the strings

Regards

1 Like

Hi,

Can you try the following expression?

System.Text.RegularExpressions.Regex.Replace(yourString,"^[\s\S]*(\w+)\s+can\s+(\w+)[\s\S]*$","$1 $2") 

Regards,

Hi @argin.lerit
Actually I want to take every 2 words from the huge text,and “овогтой” must be between 2 of them.I don’t need other words from the text.

regards,

hi @Yoichi
That was an example. I just need something like “.( овогтой ).” .
Which means the line contains “овогтой” but 2 words of both sides of it are needed.
thank you!

Hi, I think it doesn’t work.
Thank you!

How about the following?

System.Text.RegularExpressions.Regex.Replace(yourString,"^[\s\S]*\s+(\w+)\W+овогтой\W+(\w+)[\s\S]*$","$1 $2")

Regards,

2 Likes

Screenshot (32)

1 Like

It works!!!
Thanl you so much. Have a good day!!

1 Like

Hi, already got an answer.
Thanks for your kind!

Hi @Temuulen_Buyangerel

Use the below regex expression to extract the required data from the string.

System.Text.RegularExpressions.Regex.Matches(yourstringinput.ToString,“(\w+(?=\s+can)|(?<=can\s+)\w+)”)

image

Hope it helps!!

2 Likes

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