1

I have a string as below.

"Included in PTA fees" or "Included in PTA fees 33$" or "Included in PTA fees $60"

I need to match this string with searching for PTA fee/fees and whether any fee(number) is also appended with it.

Need to look for "PTA fees" along with fee value in any given string and return true.

So it should return true for

"Included in PTA fees 33$" and
"Included in PTA fees $60"

It should return false for "Included in PTA fees"

I tried with below regular expression. but it returns true for

"Included in PTA fees"

/#{PTA_FEE[0-9]}/i.match?(value)
11
  • 1
    like this Commented Apr 19, 2018 at 10:48
  • @rock321987 It wont match with "Misc charges are Included in PTA fees 33$"?? So I think we can omit ^.* Commented Apr 19, 2018 at 10:50
  • it will match.. you can check here Commented Apr 19, 2018 at 10:51
  • @rubyist: Your counter-example "Misc charges are ..." was not part of your original description. Commented Apr 19, 2018 at 10:53
  • @rock321987 /^.*#{PTA_FEE}(?=.*\d+)/.match?(value) did not match "Included in PTA fee 12$" value is "Included in PTA fee 12$" Commented Apr 19, 2018 at 10:54

4 Answers 4

2

You simply need \S+ as end part of your regex:

str =~ /Included in PTA fees? \S+/
  • \S+ One or more non-whitespace character(s)
  • ? Optional match

or to be more precise:

str =~ /Included in PTA fees? (?:\d+\$|\$\d+)/

Live demo

Sign up to request clarification or add additional context in comments.

Comments

2

This regex should work

^.*PTA fees?(?=.*(?:(?:\d+\s*\$)|(?:\$\s*\d+)))

Regex Breakdown

(?i) #Ignore Case
^ #Start of string
.*
PTA fees? #Match the string with fee(s)
(?=.* #Lookahead

  (?: #Non capturing group

     (?:\d+\s*\$) #Match digit followed by $

        | #Alternation

     (?:\$\s*\d+) #Match $ followed digit

  ) #End non capturing group
) #End lookahead

rubular demo

Comments

1

You can use this one:

^Included in PTA fees\s*(\$\d+|\d+\$)

It checks if string starts with Included in PTA fees and then allows fee number with a dollar sign before (\$\d+) or after it (\d+\$)

Demo

Comments

1

I'd go with Included in PTA fees(?=.*?(\d+(?:\.\d+)?)); this also groups the fee and takes into account floating-point values. But it doesn't take into account any free-form negation such as "aren't included in PTA fees" or "not included in PTA fees".

It disregards the dollar sign; since your description is incomplete and what you're matching against looks like something that is potentially grammatically incorrect, you may have \$\s*\d+|\d+\s*\$ for the dollar sign. Does the dollar value potentially have a thousand separator?

You can complicate the answer to this question quite a lot when the problem is under-specified.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.