Regular Expression for a currency in a statement - expression

I wanted to create a RE for currency like $123.45. It should match $123.4, $123.45 It should not match $123.456 or 123.45 I found solutions for this in this site and one of it was
^[$][0-9]+(.[0-9]{1,2})?$
the pattern as expected matches $123.4 and $123.45. But when I put the currency as part of a statement like...
"The cost of one ticket is $123.45 and the cost of 2 is $246.90" Now the pattern doesnt find any match.
I think its because of ^ and $ which are start and end of the line characters respectively.
How can I get the result as 2 matches? Please help me.

Try to remove ^ and $ from your RE. This symbols tells that search string should start with $ and end with number? Instead of them use brackets to select a group ().

Related

target of repeat operator is not specified when using optional character

I want to match a pattern like this:
ssfd
or this:
oifdsofijsdf d
So a first name alone or first name and middle initial.
"dsfsf m" =~ /^[A-Za-z]+\s[A-Za-z]$/
To make the middle initial optional, I added the ?:
"dsfsf" =~ /^[A-Za-z](+\s[A-Za-z])?$/
But it gives me error:
target of repeat operator is not specified
What am I doing wrong?
The problem is you misplaced the opening parenthesis and have a "1 or more" operator (+) right next to it, so the regex doesn't know what you can have one or more of:
^[A-Za-z]+(\s[A-Za-z])?$
Is the regex you likely intended to use (and which seems to work on Rubular for your test cases).
/^[A-Za-z]+\s*[A-Za-z]*$/
* mean length is 0 or any

REGEXP SUBSTR where delimiter is a combination of 2 or more characters

I want to search a string using a delimiter which is a combination of 2 or more characters.
Please find the query i tried below:
select REGEXP_SUBSTR('123$#45$6$#789','[^$#]+',1,2) from dual
Required Output:
45$6
Output:
45
I understand it is easily possible using user defined functions [with INSTR+SUBSTR] however I am looking for an answer & explanation with REGEXP_SUBSTR.
Thanks in advance.
Not like that... Rather:
select REGEXP_SUBSTR('123$#45$6$#789','(.*?)(\$#|$)', 1, 2, null, 1) from dual;
Notice \$ in the code; $ means end of string, so if you mean a literal dollar sign symbol, you must escape it. This solution uses the "capturing group" concept - see the Oracle documentation for REGEXP_SUBSTR() if you are not familiar. The first capturing group is (.*?) - whatever comes before the delimiter; and it is referenced in the sixth (last) argument to REGEXP_SUBSTR.
Notice also that after the first capturing group I check for either the two-character delimiter or the end of the string... which is marked by $. Two options in parentheses and separated by | (another "special character") is the regular expression syntax for "either... or...".

Regex ruby syntax to select a number while excluding specific ones

I am still struggling to find some ruby regex syntax despite the numerous documentation on-line. I have an array of string and I am looking for strings that include one number (whatever the number of digits) but not specific one (let's say for instance dates from 19XX to 201X).
I manage to get the regex for "the line contain a number"
.*\p{N}.*
I manage to get "exclude the line if this number is a year"
(?!19\d\d|20[0-1]\d)\d{4}
But I fail to combine both. I would need something that would intuitively be written as such
(.*\p{N}.*)&&(?!19\d\d|20[0-1]\d)\d{4}
But I am not sure how an AND operator can be used.
Here it is:
^(?!.*19\d\d.*)(?!.*20[01]\d.*)(.*\p{N}.*)$
You want a string that:
(?!.*19\d\d.*) doesn't contains 19xx
(?!.*20[01]\d.*) doesn't contains 200x or 201x
(.*\p{N}+.*) contains, at least, one digit
In regex && means, well, literal && and not and operator
If you want to capture numbers that are not in the range 1900-2019 you can replace with:
(?!\b19\d\d\b)(?!\b20[01]\d\b)(\b\p{N}+\b)
You can test it here
While the solution by Thomas is probably the best one, another option would be to go without negation: just select everything, that matches:
re = /\D(
[03-9]\d*|
(?:1|2|20)(?=\D)|
1[0-8]\d*|
19\d?(?=\D)|
19\d{3,}|
20[2-9]\d*|
20[01]?(?=\D)|
20[01]\d{2,}
)/x
▶ 'Here 2014 and 1945 and 1878 and 20000 and 2 and 19 and 195 and 203'.scan re
#⇒ [["1878"], ["20000"], ["2"], ["19"], ["195"], ["203"]]

Regex - How do I continue looking up for a string if a certain character ":" is found?

I wanted to check if the user inputs a correct excel column location. Normally, an excel column can have something like this:
A
B
AA
BZ
AAA
BBB
AAAA
.....
But what if I wanted to have a range of column like this:
A:F
AB:AD
CD:CF
.....
How would I create a Regular Expression that will check if a string matches first the correct column location. If a colon ":" is found, it will continue to check the rest?
So this would be the correct values:
A
AB
A:Z
BB:CC
ZA:AAA
Update 1: Here's what I've tried but it failed.
- /([a-zA-Z]{1,4}:[a-zA-Z]{1,4})/
Update 2: Here is the one I expected but can be improved.
- /([A-Z]{1,2}:?[A-Z]{1,2})/
Thanks!
I think you're close. If you add a start of line and end of line delimiter, then make the second grouping optional, you might get what you need:
^([a-zA-Z]{1,4}(?::[a-zA-Z]{1,4})?)$
The changes I made were adding ^ to the beginning, $ to the end, then surrounding the second group with (?: )? so that it doesn't pull the second group out as an individual match, and it's also optional.
Here's a Rubular link for reference: http://rubular.com/r/lVLXzMllzF

Regex capturing from a non capture group in ruby

I am trying to fix a bit of regex I have for a chatops bot for lita. I have the following regex:
/^(?:how\s+do\s+I\s+you\s+get\s+far\s+is\s+it\s+from\s+)?(.+)\s+to\s+(.+)/i
This is supposed to capture the words before and after 'to', with optional words in front that can form questions like: How do I get from x to y, how far from x to y, how far is it from x to y.
expected output:
match 1 : "x"
match 2 : "y"
For the most part my optional words work as expected. But when I pull my response matches, I get the words leading up to the first capture group included.
So, how far is it from sfo to lax should return:
sfo and lax.
But instead returns:
how far is it from sfo and lax
Your glitch is that the first chunk of your regex doesn't make sense.
To choose from multiple options, use this syntax:
(a|b|c)
What I think you're trying to do is this:
/^(?:(?:how|do|I|you|get|far|is|it|from)\s+)*(.+)\s+to\s+(.+)/i
The regexp says to skip all the words in the multiple options, regardless of order.
If you want to preserve word order, you can use regexps such as this pseudocode:
… how (can|do|will) (I|you|we) (get|go|travel) from …
When you want to match words, \w is the most natural pattern I'd use (e.g., it is used in word count tools.)
To capture any 1 word before and after a "to" can be done with (\w+\sto\s+\w*) regex.
To return them as 2 different groups, you can use (\w+)\s+to\s+(\w+).
Have a look at the demo.

Resources