0

I am trying to use regular expressions in Java that are built for Perl's regex. My goal is to have a standardized set of regular expressions so that both our Java apps and Perl scripts are producing the same results when applying the regular expressions.

Is there a library that will take a perl regex such as: \bbypa(s|r)?\b and use it directly in Java or do I need to escape special characters for each regular expression?

If I have to implement the second method (which I hope I can avoid), what are the characters that need to be escaped to function in Java?

Any insight into this will be helpful

Thanks!!

EDIT:

I have over 500 of these regular expressions sitting in the database. They all work perfect on perl. Do I just need to escape the backslashes when using these regex in Java and assume it produces the same results as Perl?

Mos No
  • 159
  • 1
  • 14
  • I was hoping to get an answer to whether there is an open source library that converts Perl's regex to Java syntax – Mos No Jul 03 '14 at 15:46
  • In a regular expression every literal `'\'` must be doubled `'\\'`. You can use [RegexBuddy](http://www.regexbuddy.com/) to do this. – hwnd Jul 03 '14 at 15:48
  • [http://stackoverflow.com/questions/6933891/tool-to-convert-regex-between-different-language-syntaxes](http://stackoverflow.com/questions/6933891/tool-to-convert-regex-between-different-language-syntaxes) – hwnd Jul 03 '14 at 15:53

2 Answers2

0

Your java regex would be,

"\\bbypa(s|r)?\\b"

You need to escape the backslash \ one more time. Just put your perl regex into this javaregex online checker site and click on test button. You would get a pure java regex code.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

You can use that expression, in Java you must double escape the backslash escape sequences.

 \\bbypa(s|r)?\\b
user3077033
  • 167
  • 2
  • 8