Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Go mismatch [duplicate]

Tags:

regex

go

I have developed some regex in regexr where it works as expected, but when I use it in Go it seems to be mismatching strings.

(\+|-)?(((\d{1,3}[, ])(\d{3}[ ,])*\d{3})|\d+)( ?[\.,] ?(\d{3}[, ])*\d+)?

For example in regexr the following input does not match:

1.12,4.64

But in Go it does match.

like image 409
pcwizz Avatar asked Feb 11 '15 12:02

pcwizz


People also ask

What is ?! In regex?

It's a negative lookahead, which means that for the expression to match, the part within (?!...) must not match. In this case the regex matches http:// only when it is not followed by the current host name (roughly, see Thilo's comment).

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

How do I not match a character in regex?

There's two ways to say "don't match": character ranges, and zero-width negative lookahead/lookbehind. Also, a correction for you: * , ? and + do not actually match anything. They are repetition operators, and always follow a matching operator.

What is multiline regex?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.


1 Answers

^(\+|-)?(((\d{1,3}[, ])(\d{3}[ ,])*\d{3})|\d+)( ?[\.,] ?(\d{3}[, ])*\d+)?$

Try with anchors.^$ will disable partial matching.See demo.

https://regex101.com/r/qH1uG3/4

like image 174
vks Avatar answered Oct 06 '22 02:10

vks