How do I capture the text that is before and after a multiple regex matches in java? -



How do I capture the text that is before and after a multiple regex matches in java? -

given test string of:

i have 1234 , 2345 , maybe 3456 id.

i match ids (the 4 digit numbers) , @ same time 12 characters of surrounding text (before , after) (if any!)

so matches should be:

class="lang-none prettyprint-override"> before match after match #1: have a- 1234 -and 2345- match #2: -1234 , a- 2345 -and maybe match #3: , maybe a- 3456 -id. (-) space character note:

the before match of match #1 not 12 characters long (not many characters @ origin of string). same after match of match #3 (not many characters after lastly match)

can accomplish these matches single regex in java?

my best effort far utilize positive behind , atomic grouping (to surrounding text) fails in origin , end of string when there not plenty characters (like note above)

(?<=(.{12}))(\d{4})(?>(.{12}))

this matches 2345. if utilize little plenty value quantifiers (2 instead of 12, example) correctly match ids.

here link regex playground trying regex's:

http://regex101.com/r/cz6wg4

when @ matchresult (http://docs.oracle.com/javase/7/docs/api/java/util/regex/matchresult.html) interface implemented matcher class (http://docs.oracle.com/javase/7/docs/api/java/util/regex/matcher.html) find functions start() , end() give index of first / lastly character of match within input string. 1 time have indicies, can utilize simple math , substring function extract parts want.

i hope helps you, because won't write entire code you.

there might possibility want purely regex. think using indicies , substring easier (and more reliable)

java regex

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -