Skip to content

Commit

Permalink
Regex commits for equations parser (#60)
Browse files Browse the repository at this point in the history
# Description ✍️

Creates regex(input, pattern) function

# Overview 🔍

```c
regex("2 dogs are walking in the streets", "(\\d+ \\w+)") # => "2 dogs"
```

# Checks ☑️

- Regex commits for equations parser
- Add regex tests
  • Loading branch information
niltonvasques authored Apr 21, 2023
1 parent 26ddcf8 commit f500d7a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ext/libnativemath/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
end

GIT_REPOSITORY = 'https://github.com/oxeanbits/equations-parser.git'.freeze
COMMIT = '8c4fab5b8949704cf4d80d4a4a898f451cbb3890'.freeze
COMMIT = '8769175390cafd9c40b52477f1c97184266c4a68'.freeze

Dir.chdir(BASEDIR) do
system('git init')
Expand Down
6 changes: 3 additions & 3 deletions parsec.gemspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Gem::Specification.new do |s|
s.name = 'parsecs'
s.version = '0.11.9'
s.version = '0.12.0'
s.platform = Gem::Platform::RUBY
s.authors = ['Nilton Vasques', 'Victor Cordeiro', 'Beatriz Fagundes']
s.email = ['[email protected]', '[email protected]', '[email protected]']
s.description = 'ParseCs is a gem to evaluate equations using a lighter and extented version of the muparserx C++ library'
s.homepage = 'https://github.com/niltonvasques/parsec'
s.description = 'Parsecs is a gem to evaluate equations using a lighter and extented version of the muparserx C++ library'
s.homepage = 'https://github.com/oxeanbits/parsec'
s.summary = 'A gem to evaluate equations using muparserx C++ library'
s.files = ['lib/parsec.rb', 'lib/string_to_boolean_refinements.rb', 'ext/libnativemath/libnativemath.i',
'ext/libnativemath/libnativemath.cpp', 'ext/libnativemath/libnativemath.h']
Expand Down
36 changes: 36 additions & 0 deletions test/test_parsec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,40 @@ def test_timediff
assert_raises(SyntaxError) { parsec.eval_equation('timediff("02:00:00", "02:30:62")') }
assert_raises(SyntaxError) { parsec.eval_equation('timediff("24:00:00", "02:30:59")') }
end

def test_regex
parsec = Parsec::Parsec
assert_equal('World', parsec.eval_equation('regex("Hello World", "Hello (.*)")'))
assert_equal('71', parsec.eval_equation('regex("71 123456789", "([0-9]{2}) [0-9]{9}")'))
assert_equal('ISSUE-123', parsec.eval_equation('regex("ISSUE-123 Fix bug", "(ISSUE-[0-9]+) (.*)")'))
assert_equal('2019-01-01', parsec.eval_equation('regex("2019-01-01T:08:30", "([0-9]{4}-[0-9]{2}-[0-9]{2})T:[0-9]{2}:[0-9]{2}")'))
assert_equal('2019-01', parsec.eval_equation('regex("2019-01-01T:08:30", "([0-9]{4}-[0-9]{2})-[0-9]{2}T:[0-9]{2}:[0-9]{2}")'))
assert_equal('2019', parsec.eval_equation('regex("2019-01-01T:08:30", "([0-9]{4})-[0-9]{2}-[0-9]{2}T:[0-9]{2}:[0-9]{2}")'))
# Regex tests with no capture group
assert_equal('', parsec.eval_equation('regex("Hello World", "Hello .*")'))

# Regex tests with optional groups
assert_equal('1234', parsec.eval_equation('regex("Product 1234 (color: red)", "Product ([0-9]+)( \\\\(color: (.*)\\\\))?")'))
assert_equal('1234', parsec.eval_equation('regex("Product 1234", "Product ([0-9]+)( \\\\(color: (.*)\\\\))?")'))

# Regex tests with alternation
assert_equal('Green', parsec.eval_equation('regex("Green Apple", "(Green|Red) Apple")'))
assert_equal('Red', parsec.eval_equation('regex("Red Apple", "(Green|Red) Apple")'))

# Regex tests with character classes
assert_equal('A123', parsec.eval_equation('regex("BoxA123", "Box([A-Za-z][0-9]+)")'))
assert_equal('C456', parsec.eval_equation('regex("BoxC456", "Box([A-Za-z][0-9]+)")'))

# Regex tests with positive lookaheads
assert_equal('apple', parsec.eval_equation('regex("apple123banana", "([a-z]+)(?=\\\\d+)")'))

assert_equal('456', parsec.eval_equation('regex("123red456blue", "(\\\\d+)(?=blue)")'))

# Regex tests with negative lookaheads
assert_equal('123', parsec.eval_equation('regex("123red456blue", "(\\\\d+)(?!blue)")'))

# Regex tests with nested lookaheads
assert_equal('apple', parsec.eval_equation('regex("apple123redbanana", "(?=apple)([a-z]+)(?=\\\\d+red)")'))
assert_equal('apple', parsec.eval_equation('regex("apple123red456banana", "(?=apple)([a-z]+)(?=(\\\\d+red)\\\\d+banana)")'))
end
end

0 comments on commit f500d7a

Please sign in to comment.