Reputation: 59
I am using regexp heavily in my project. I need some suggestions for Test strings:
1 string 3.33 string 1 string -3.33
I need to match the 2nd and 3rd lines (means I do not need string which has 3.33 (currency) at the end of the line). I tried so many variations. The best I got is:
^[\s]+.+[^(?!(\d+\.\d+))]$
Line 2 matches with this regular expression, but line 3 does not match.
Note: I do care about the beginning or end of the line. So the test lines marked above are with perfect whitespaces.
I use Java as my programming language.
Upvotes: 1
Views: 195
Reputation: 75222
[^(?!(\d+\.\d+))]
is a character class. A character class matches exactly one character from the set of characters you describe within the square brackets. Yours is equivalent to this:
[^!()+.\d]
The ^
at the beginning inverts the set, and \d
matches a digit just like it does outside a character class, but the rest of the characters are matched literally. In other words, you're telling it to match any one character that's not !
, (
, )
, +
, .
, or a digit.
It looks like you were trying to use a negative lookahead, which is a valid approach. If you only care about the dollar amount at the end of the line, you can do this:
^(?!.*\d+\.\d+$).*$
The lookahead tries to match \d+\.\d+
at the end of the line. If it succeeds, the overall match fails. Otherwise, the .*$
consumes the whole line so you can retrieve it with the Matcher's group()
method.
This assumes you're applying the regex to one line at a time. If you're trying to find matching lines within a larger text you should specify MULTILINE mode, which you can do like this:
(?m)^(?!.*\d+\.\d+$).*$
Upvotes: 1
Reputation: 195039
did you test the text line by line?
then you may use re: \d+\.\d+$
to match the text which you do NOT need. If match() return false, then you take the line.
well it's like grep -v.
if test it with grep:
kent$ cat a
1 string 3.33
string
1
string -3.33
kent$ grep -Pv '\d+\.\d+$' a
string
1
Upvotes: 0