i new regex, learning now. have requirement this:
any string starts #newline# , ends #newline#. in between these 2 words, there (0 or more spaces) or (0 or more #newline#).
below example:
#newline# #newline# #newline##newline# #newline##newline##newline#.
how regex this?
i have tried this, not working
^#newline#|(\s+#newline#)|#newline#|#newline#$
your ^#newline#|(\s+#newline#)|#newline#|#newline#$
matches either #newline#
@ start of string (^#newline#
), or 1+ whitespaces followed #newline#
((\s+#newline#)
), or #newline#
, or (and never matches previous catches cases of #newline#
) #newline#
@ end of string (#newline#$
).
you may match these strings
^#newline#(?:\s*#newline#)*$
or (if there should @ least 2 occurrences of #newline#
in string)
^#newline#(?:\s*#newline#)+$ ^
see regex demo.
^
- start of string#newline#
- literal string(?:\s*#newline#)*
- 0 (note: replacing*
+
require @ least 1) or more sequences of\s*
- 0+ whitespaces#newline#
- literal substring
$
- end of string.
string s = "#newline# #newline# #newline##newline# #newline##newline##newline#"; system.out.println(s.matches("#newline#(?:\\s*#newline#)+")); // => true
note: inside matches()
, expression anchored, , ^
, $
can removed.
Comments
Post a Comment