1

I'm not a pro in Regex, therefore I'm having a difficulty translating this regex pattern to Java.

I believe it would be an easy task to a guy which is familiar enough with Regex. I have seen similar topics, however - each topic is relevant for a specific regex pattern...

The Pattern is:

@"\<meta name=""title"" content=""(?<title>.*)""\>"

Thanks in advance!

Steve
  • 203,265
  • 19
  • 210
  • 265
Nati Cohen
  • 230
  • 4
  • 10
  • The only thing interesting I see in there is the double-quotes. Remove the `@` at the front, un-double the quotes and escape them instead (with a backslash). – mpen Mar 25 '12 at 17:57
  • I did so. I didn't just copy-paste it to Java... I removed the '@' and changed the special characters to \* (e.g. \" instead of "" and \\ instead of \), and yet I got an exception that this is not a valid Regex Pattern – Nati Cohen Mar 25 '12 at 18:00
  • 2
    It's probably the named capture. I cant find any straight answer on if Java supports them. Try taking it out. Replace `(?.*)` with just `(.*)` (Edit: Actually, apparently Java7 does but not prior http://stackoverflow.com/a/415635/65387) – mpen Mar 25 '12 at 18:06

1 Answers1

1

If you want slashes in front of the openning and closing angle-brackets, use this (escaped for Java):

"\\\\<meta name=\"title\" content=\"(.*)\"\\\\>"

Without them, use this:

"<meta name=\"title\" content=\"(.*)\">"

Tested here using the unescaped version:

<meta name="title" content="(.*)">
mpen
  • 237,624
  • 230
  • 766
  • 1,119
  • I have tried it, and got an exception. just to be sure, I have tested it on http://regex.powertoy.org/ as-well, and got a negative result. – Nati Cohen Mar 25 '12 at 18:08