java_regex 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/java_regex/exceptions.rb +5 -0
- data/lib/java_regex/translator.rb +69 -0
- data/lib/java_regex.rb +19 -0
- metadata +49 -0
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding=utf-8
|
2
|
+
class JavaRegex::Translator
|
3
|
+
def self.translate(regex)
|
4
|
+
regex = String.new(regex)
|
5
|
+
characters(regex)
|
6
|
+
character_classes_or_character_sets(regex)
|
7
|
+
word_boundaries(regex)
|
8
|
+
modifiers(regex)
|
9
|
+
unicode_characters!(regex)
|
10
|
+
posix_bracket_expressions!(regex)
|
11
|
+
regex.sub!(/\A(.*)\z/, '\A\1\z')
|
12
|
+
return regex
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.characters(regex)
|
16
|
+
meta_char_start = regex.scan('\Q')
|
17
|
+
meta_char_end = regex.scan('\E')
|
18
|
+
control_char = regex.scan(/\\c[[:upper:]]/)
|
19
|
+
raise JavaRegexException::TranslationError,
|
20
|
+
'"\Q" and "\E" are not supported' unless
|
21
|
+
meta_char_start.empty? and
|
22
|
+
meta_char_end.empty?
|
23
|
+
raise JavaRegexException::TranslationError,
|
24
|
+
'"\cA" through "\cZ" are not supported' unless
|
25
|
+
control_char.empty?
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.character_classes_or_character_sets(regex)
|
29
|
+
literal_hyphen = regex.scan(/\[\\.-.\]/)
|
30
|
+
raise JavaRegexException::TranslationError,
|
31
|
+
'Literal hyphens like [\d-z] are not supported' unless
|
32
|
+
literal_hyphen.empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.word_boundaries(regex)
|
36
|
+
non_ascii = regex.scan(/\\[bB][[:print:][:cntrl:]]/)
|
37
|
+
raise JavaRegexException::TranslationError,
|
38
|
+
'Non-ascii characters following "\b" or "\B" are not supported' unless
|
39
|
+
non_ascii.empty?
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.modifiers(regex)
|
43
|
+
newline = regex.scan(/\\n/)
|
44
|
+
raise JavaRegexException::TranslationError,
|
45
|
+
'Newlines are currently not supported' unless
|
46
|
+
newline.empty?
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.unicode_characters!(regex)
|
50
|
+
re = /\\u([[:xdigit:]]{4})/
|
51
|
+
regex.gsub!(re) { |m| m.gsub!(/\A.*\z/, [Integer("0x#{$1}")].pack('U*')) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.posix_bracket_expressions!(regex)
|
55
|
+
regex.gsub!('\p{Lower}', '[[:lower:]]')
|
56
|
+
regex.gsub!('\p{Upper}', '[[:upper:]]')
|
57
|
+
regex.gsub!('\p{Alpha}', '[[:alpha:]]')
|
58
|
+
regex.gsub!('\p{Digit}', '[[:digit:]]')
|
59
|
+
regex.gsub!('\p{Alnum}', '[[:alnum:]]')
|
60
|
+
regex.gsub!('\p{Punct}', '[[:punct:]]')
|
61
|
+
regex.gsub!('\p{Graph}', '[[:graph:]]')
|
62
|
+
regex.gsub!('\p{Print}', '[[:print:]]')
|
63
|
+
regex.gsub!('\p{Blank}', '[[:blank:]]')
|
64
|
+
regex.gsub!('\p{Cntrl}', '[[:cntrl:]]')
|
65
|
+
regex.gsub!('\p{Space}', '[[:space:]]')
|
66
|
+
regex.gsub!('\p{XDigit}', '[[:xdigit:]]')
|
67
|
+
regex.gsub!('\p{ASCII}', '[[:print:][:cntrl:]]')
|
68
|
+
end
|
69
|
+
end
|
data/lib/java_regex.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding=utf-8
|
2
|
+
class JavaRegex
|
3
|
+
attr_accessor :regex
|
4
|
+
|
5
|
+
def initialize(regex)
|
6
|
+
@regex = regex
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_ruby
|
10
|
+
Translator.translate(@regex)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_ruby_regex
|
14
|
+
Regexp.new(to_ruby())
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'java_regex/exceptions'
|
19
|
+
require 'java_regex/translator'
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: java_regex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dean Morin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! " For the most common cases, either converts to the Ruby equivalent,
|
15
|
+
or\n throws an exception if conversion is not supported.\n"
|
16
|
+
email: morin.dean@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/java_regex.rb
|
22
|
+
- lib/java_regex/translator.rb
|
23
|
+
- lib/java_regex/exceptions.rb
|
24
|
+
homepage: http://github.com/deanmorin/java_regex
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ! '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 1.8.23
|
46
|
+
signing_key:
|
47
|
+
specification_version: 3
|
48
|
+
summary: Converts Java regexes to Ruby regexes
|
49
|
+
test_files: []
|