mhollander38
mhollander38

Reputation: 775

Java Regex Forward Slash Causing Match Not To Be Made

We are trying to make a match on any text between a triplet of "s for example

""" Some text here """

We want to capture Some text here. This we have working alright, however, we have an issue when the text between the triplet of "s contains a forward slash /. So for example """ This doesn/'t work """, so if we have have;

""" This doesn/'t work """ """ Some text here """

Then the match goes and ignores the first """ and goes on to match up to the second closing """ so the match becomes;

This doesn/'t work Some text here

When all we want is This doesn/'t work. The regex I am using is as follows;

(?:"{3})([\p{Alnum}|\p{Punct}|\p{Space}]*)(?:"{3})

We are using capturing groups as a note.

Upvotes: 1

Views: 853

Answers (2)

ioreskovic
ioreskovic

Reputation: 5699

This gives desired result (tested in rubular.com):

Regex: """\s*([\S]+)\s+([\S]+)\s+([\S]+)\s*"""
Input: """ This doesn/'t work """ """ Some text here """
Match: """ This doesn/'t work """, """ Some text here """

Match groups:

Match 1
1.  This
2.  doesn/'t
3.  work
Match 2
1.  Some
2.  text
3.  here

Upvotes: 0

nhahtdh
nhahtdh

Reputation: 56809

Seems like problem with greedy/lazy quantifier. Change * to *? for lazy matching.

i.e. (?:"{3})([\p{ALnum}|\p{Punct}|\p{Space}]*?)(?:"{3})

Upvotes: 1

Related Questions