java - Regex match certain IDs -


i need tricky regex solve (for me!) , hope can learn write myself in future.

i need match of following ids:

#1 #12 #123 #1234 #5069 #316& #316. #316; 

and not want match leading zeros , numbers end ] or [ or between ().

#0155 #0000155 #1123] #1123[ (#1125) 

i have come this: (#[1-9]\d{0,}), matches of above. so, tried different stuff like:

"(#[1-9]\\d{0,})([\\s,<\\.:&;\\)])" "(#[1-9]+)([\\s,<\\.])" "(?m)(#[1-9]+)(.,\(,\))" 

but want (#[1-9]\d{0,}) match numbers but not following [ or ] or ( or ).

how can express in regex?

p.s.: regex needs used in java.

maybe can solve this, better if can explain how got way solution, can learn new , others when struggle same problem.

kind regards!

you may use possesive quantifier.

"#[1-9]\\d*+(?![\\[\\])])" 

\\d*+ matches 0 or more character greedily , + eixts after * won't let regex engine backtrack.

add optional \\w, if want match following non-word character.

"#[1-9]\\d*+(?![\\[\\])])\\w?" 

demo


Comments