I'm trying to convert $(5.20) to -5.20 using regular expressions, but can't seem to figure out how to use regular expressions. Could someone please help me to resolve this issue? I need to remove the dollar sign and only accept digits with a maximum of 1 decimal point ie do not accept 5.2.0, but only 5.20. Also, the $ at the beginning is optional and there could be multiple $ as well.
Example of what I started with:
^\((\d)*\)$ - does not work
-$1
Try this regex
^\$\((\d+\.\d+)\)$
Your replacement should already be fine. This regex expects at least one digit before and after the decimal point.
The problem with your regex is, that it will only match if there is no decimal point at all. Also you don't check for the $ character. And lastly, if this $(5.20) is not your full string, than you should leave out the anchors at the beginning and at the end:
\$\((\d+\.\d+)\)
If you want to match numbers without decimal points, too, then you can make the dot and the second repetition optional:
\$*?\((\d+\.?\d*)\)
Related
I am trying to write a regex to get only integer numbers e.g, 23, 234, 45, etc, and not select numbers with decimal points.
For Context :
I need it in a larger regex that I am writing to convert mixed fraction latex input
For example:
5\frac{7}{8}
But it should not select latex such as:
3.5\frac{7}{8}
The regex That I have so far is:
(^(.*)(?!(\.))(.*))\\frac{([^{}]+(?:{(?:[^{}]+)}|))}{([^{}]+(?:{(?:[^{}]+)}|))}
But it is for integer and decimal numbers alike. Need to change the regex for group1.
Maybe this will do it for you:
(?<!\d\.)\b(\d+)\\frac{([^{}]+(?:{(?:[^{}]+)}|))}{([^{}]+(?:{(?:[^{}]+)}|))}
It captures an integer expression before \fraq, unless it's preceded by a digit and a full stop.
(?<!\d\.) This ensures the number isn't preceded by a digit followed by a full stop, i.e.
the integer part of a floating.
\b Must be at the start of a number (to make sure we don't get a match with the end
of a multi digit number).
(\d+) Captures the integer number
\\frac Matches the string "\fraq"
The rest is the same as you original expression.
See it here at regex101.
Edit
Since there obviously are people out there, however unbelievable, that still haven't moved to a real browser ;) - the answer has to change to:
It depends of the syntax of latex, whether you can do it or not.
(And since I don't know that, I shouldn't have said anything in the first place ;)
The problem is that, without look behinds, you can't do it without matching characters outside the expression your interested in. In your regexr-example you clearly show that you want to be able to match expression not only at the beginning of strings, but also in the middle of the. Thus we need to be able to tell that the part before our match isn't the integer part of a decimal number. Now, doing this with matching isn't a problem. E.g.
(?:^|[^\d.])(\d+)\\frac{...
like Wiktor suggested in comments, will match if the expression is on the start of the line (^), or is preceded by something that isn't a decimal point or a digit ([^\d.]). That should do it. (Here at regex101.)
Well, as pointed out earlier, it depends on the syntax of latex. If two expressions can be directly adjacent, without any operators or stuff between them, you can't (as far as I can tell) do it with JS regex. Consider 1\fraq{0}{1}2\fraq{2}{3} (which I have no idea if it's syntactically correct). The first expression 1\fraq{0}{1} is a piece of a cake. But after that has been matched, we'd need to match a character before the second expression to verify the it doesn't start with a decimal number, but since the first expression already ate the characters, we can't. Because the test (?:^|[^\d.]) to verify that our expression doesn't start with a decimal number, would match one of the characters that actually belongs to our expression (the 2 in 2\fraq{2}{3}), thus making the match fail, because the remaining part doesn't start with the digit needed to satisfy the rest of the regex (\d+)\\frac{....
If, however, an expression always starts the string tested, or is preceded by and operator, or such, then it should be possible using
(?:^|[^\d.])(\d+)\\frac{([^{}]+(?:{(?:[^{}]+)})?)}{([^{}]+(?:{(?:[^{}]+)})?)}
Here at regex101.
(Sorry for my rambling)
I'm trying to write a regular expression to match amounts. In my case, what I need is that either the amount should be a positive integer or if the decimal is used, it must be followed by one or two integers. So basically, the following are valid amounts:
34000
345.5
876.45
What I wrote was this: /[0-9]+(\.[0-9]{1,2}){0,1}/
My thinking was that by using parenthesis like so: (\.[0-9]{1,2}), I would be able to bundle the whole "decimal plus one or two integers" part. But it isn't happening. Among other problems, this regex is allowing stuff like 245. and 345.567 to slip through. :(
Help, please!
Your regular expression is good, but you need to match the beginning and end of the string. Otherwise, your regex can match only a portion of the string and still (correctly) return a match. To match the beginning of the string, use ^, for the end, use $.
Update: as Avinash has noted, you can replace {0,1} with ?. JS supports \d for digits, so the regex can be further simplified
Finally, since if are only testing against a regex, you can use a non-capturing group ( (?:...) instead of (...)), which offers better performance.
original:
/[0-9]+(\.[0-9]{1,2}){0,1}/.test('345.567')
Fixed, and faster ;)
/^\d+(?:\.\d{1,2})?$/.test('345.567')
Here is the pattern on which i'm Working :
/^\d+((.\d+))?(,\d+((.\d+))?)*$/
Its supports
1.2,30.5,13.54
25.65
But i want the pattern which supports following:
1.3,.3,4.5
.3,.4,.6
.2
1.3,5.6,.5
Based on your given examples, you require a decimal part, so you could use
/(\d*\.\d+),?/
This will match every given example.
In case you also want to match numbers without a decimal dot, just add a second match:
/(\d*\.\d+|\d+),?/
Oh, and for JavaScript, to add the "g" modifier (for global search), you need to add it as a second parameter to the RegExp constructor:
re = new RegExp("(\d*\.\d+|\d+),?", "g");
Example: http://regex101.com/r/vL5aT0
Okay, I don't know on what purpose you are matching your strings. If you just want to validate them and they should exactly look like in your examples, use this:
/((\d*\.\d+|\d+),?)*(\d*\.\d+|\d+)$/
Thanks to Elergy for pointing me to this.
By the way, Elergy's regex also matches useless lines of only periods and commas, like
.3,.4,.5,,,,8,.,7.,.,.,.,4.,.,.,.,.9,.,,,4.,,1,,
/(\d+)?\.\d+(,?)/
(\d+)? Match all digits but optional.
\d+(,?) Match all digits with a , as optional.
If my understanding of your problem is right, this expression can help you:
/^\d*[\.,]\d*(?:,\d*[\.,]\d*)*$/
What do you want to do with them afterwards? If you goal is to identify a comma separated list of valid floating point numbers, you can use the Number constructor, which returns NaN if the number is invalid (which evaluates to false):
"1.3,5.6,a.s".split(",").every(Number)
false
"1.3,5.6,.5".split(",").every(Number)
true
Personally I like to avoid regex where possible and I think that this is pretty self-documenting. split the string on , and check that every value is a valid number.
Learning regex but this one gives me a headache. I need to match a float number (with either . or , as decimal point) and it MUST end with the following characters: €/g.
Valid matches should be for example:
40€/g
43.33€/g
40,2€/g
40.2€/g
38.943€/g
Appreciate help..
The regex will look like:
\d+(?:[.,]\d+)?€/g
In Javascript, as a regex object (note that the forward slash needs to be escaped):
/\d+(?:[.,]\d+)?€\/g/
Here's a breakdown of what each part does:
\d+ # one or more digits
(?: # ... don't capture this group separately
[.,] # decimal point
\d+ # one or more digits
)? # make the group optional
€/g # fixed string to match
If you want to allow something like .123€/g to be valid as well, you can use:
(?=[.,]|\d)(?:\d+)?(?:[.,]\d+)?€/g
That is, both the groups of digits are optional, but at least one must be present (this uses lookahead, which is a bit more tricky).
Note that this will also match constructions like 'word2€/g'. If you want to prevent this, start the regex with (?<=^|\s) (matches if preceded by a space or the start of the string) and end it with (?=$|\s) (matches if followed by a space or the end of the string).
Full-blown version:
(?<=^|\s)(?=[.,]|\d)(?:\d+)?(?:[.,]\d+)?€/g(?=$|\s)
\d+([.,]\d+)?€/g
should work, I guess.
Are you really sure you need a regex for this? It might be easier to instead leverage the builtin floating point parsing that is available: take whatever comes before the euro sign, normalize commas to decimals (or vice versa, whatever ends up working) and then try to parse it with the Number function. Note that you would need to check if the conversion worked with the Number.isNaN function.
Another possibility is to just use the parseFloat function. Since it ignores any characters after the numbers then it would parse "40€ as 40.0. However, it might not be what you want since it would also allow things like "40a" and "40b" as well.
I'm trying to create a regular expression in javascript for a UK bank sort code so that the user can input 6 digits, or 6 digits with a hyphen between pairs. For example "123456" or "12-34-56". Also not all of the digits can be 0.
So far I've got /(?!0{2}(-?0{2}){2})(\d{2}(-\d{2}){2})|(\d{6})/ and this jsFiddle to test.
This is my first regular expression so I'm not sure I'm doing it right. The test for 6 0-digits should fail and I thought the -? optional hyphen in the lookahead would cause it to treat it the same as 6 0-digits with hyphens, but it isn't.
I'd appreciate some help and any criticism if I'm doing it completely incorrectly!
Just to answer your question, you can validate user input with:
/^(?!(?:0{6}|00-00-00))(?:\d{6}|\d\d-\d\d-\d\d)$/.test(inputString)
It will strictly match only input in the form XX-XX-XX or XXXXXX where X are digits, and will exclude 00-00-00, 000000 along with any other cases (e.g. XX-XXXX or XXXX-XX).
However, in my opinion, as stated in other comments, I think it is still better if you force user to either always enter the hyphen, or none at all. Being extra strict when dealing with anything related to money saves (unknown) troubles later.
Since any of the digits can be zero, but not all at once, you should treat the one case where they are all zero as a single, special case.
You are checking for two digits (\d{2}), then an optional hyphen (-?), then another two digits (\d{2}) and another optional hyphen (-?), before another two digits (\d{2}).
Putting this together gives \d{2}-?\d{2}-?\d{2}, but you can simplify this further:
(\d{2}-?){2}\d{2}
You then use the following pseudocode to match the format but not 000000 or 00-00-00:
if (string.match("/(\d{2}-?){2}\d{2}/") && !string.match("/(00-?){2}00/"))
//then it's a valid code, you could also use (0{2}-?){2}0{2} to check zeros
You may wish to add the string anchors ^ (start) and $ (end) to check the entire string.