Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbol OR end of line pattern

Tags:

java

regex

I need a pattern (java regexp) which matches if there is a comma after key or key is at the end of line. i.e it should match both of followings:

1. xxxkey,yyy
2. xxxkey

Ive tried [\\,$] pattern but it doesn't work.

like image 433
shift66 Avatar asked Oct 18 '11 14:10

shift66


People also ask

What breed would match the end of a line?

' \\$ ' This matches a string ending with a single backslash. The regexp contains two backslashes for escaping.

Why * is used in regex?

- a "dot" indicates any character. * - means "0 or more instances of the preceding regex token"

What is G at end of regex?

RegExp. prototype. global has the value true if the g flag was used; otherwise, false . The g flag indicates that the regular expression should be tested against all possible matches in a string.

Which special character will anchor the expression to the end of the line?

The caret (^) is the starting anchor, and the dollar sign ($) is the end anchor.


1 Answers

$ inside a character class loses its special meaning. Use the following instead:

key(,|$)

If you don't need to know whether there was a comma, you can use a non-capturing group instead:

key(?:,|$)
like image 101
NPE Avatar answered Oct 06 '22 17:10

NPE