How to use regular expressions?

Asked

Viewed 76 times

-4

I have a string with this value:

Romania","PROXY_IP":"93.118.243.19","PROXY_LAST_UPDATE Indonesia","PROXY_IP":"117.102.88.121","PROXY_LAST_UPDATE Russia","PROXY_IP":"194.135.97.178","PROXY_LAST_UPDATE Malaysia,"PROXY_IP":"192.228.193.78","PROXY_LAST_UPDATE

and find all parts with " "PROXY_IP":"XXX.XX.XXX.XXX" " Code I tried to:

static Regex ipR = new Regex(@""PROXY_IP":"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}"");

but everything turned red... I don’t know how to make Regex search for quotes in the middle of the string too

  • 1

    Take a look at this link https://answall.com/questions/172741/como-indica-numa-regex-que-os-s%C3%Admbolos-e-os-par%C3%Aanteses-s%C3%A3o-uma-das-al in the quote-escaping example, I think it will help you a lot

  • @Luizaugusto thanks for the reply, but it did not help much since expressions in java is different than in c#

  • I don’t program much in C# but I believe the problem is in the quotes same. As for the IP, I think we can "simplify" to \d{1,3}(\.\d{1,3}){3} - don’t forget to escape the point too, because . means "any character", while \. means "point". Of course you can complicate even more if you want a regex that only accepts numbers between zero and 255

  • Another link, http://aurelio.net/regex/c/ is the complete material of the Aurelio.

  • I don’t know how you define the best answer, but thank you @hkotsubo

1 answer

2


To use quotation marks on a Regex, you need to use the backslash ( ). I mean, your regex would look like this:

(\"PROXY_IP\":\"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\")

There are some very good teaching regex sites that are interactive. Take a look at Regexr, created an example using your case.

  • 1

    Yes, I studied a little and understood the "logic of regex" haha Thank you! The doubt has been resolved

Browser other questions tagged

You are not signed in. Login or sign up in order to post.