0

Unable to implement regex for string in line starts with ? and ends with ; appended with single quotes or double quotes.

for example:

?abcdef;
'?abcdef;'
"?abcdef;" 

I tried a lot, like this "^\\?([^;]+)\\;$" but it did not work.

Test code snippet:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(final String[] args) {

        final String text = "This is param-start ?abcdef; param-end";

        final String patternString = "(['\"]?)\\?.*;\\1";

        final Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
        final Matcher matcher = pattern.matcher(text);

        System.out.println("lookingAt = " + matcher.lookingAt());
        System.out.println("matches   = " + matcher.matches());
    }
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Girish
  • 1,717
  • 1
  • 18
  • 30
  • 1
    Maybe `^(['"]?)\?.*;\1$` will do? See [this regex demo](https://regex101.com/r/psUs84/2). Java: `s.matches("(['\"]?)\\?.*;\\1")`. – Wiktor Stribiżew Apr 18 '19 at 20:14
  • 1
    Please add a code snippet showing how you test the regex to the question. – Wiktor Stribiżew Apr 18 '19 at 20:20
  • added code snippet @Wiktor – Girish Apr 18 '19 at 20:28
  • 2
    You seem to need a partial match, so use it with `.find()`, `while (matcher.find()) { System.out.println("matches = " + matcher.group()); }`, and the regex should be `String patternString = "(['\"]?)\\?.*?;\\1"`. See [this Java demo](https://ideone.com/OKe9kW). – Wiktor Stribiżew Apr 18 '19 at 20:34
  • What do you expect to get? If your string has line breaks, use the `"(?s)(['\"]?)\\?.*?;\\1"` regex, but it is not clear what you call "not working" and how to make it "working" for you. – Wiktor Stribiżew Apr 18 '19 at 20:45
  • it is working Wiktor Thanks a lot, I am trying one more regex will take a help from u if not able to design thanks – Girish Apr 18 '19 at 20:54

3 Answers3

4

You may use this regex:

(["']?)\?.*;\1

For Java use:

final String pattern = "(['\"]?)\\?.*;\\1";

RegEx Demo

  • (["']?) matches an optional ' or " and captures in group #1
  • \1 is back-reference for the same value as in group #1
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Your description stated "starts with ? and ends with ;", but your sample string does not end with a semicolon - it's ending with wtih a double quote.

If you want to allow the string to start or end with either a single quote or double quote, your match should be: ^['"]?\?[^;]+;\['"]?$

0

You may use

final String text = "This is param-start ?abcdef; param-end";
final String patternString = "(['\"]?)\\?.*?;\\1";
final Pattern pattern = Pattern.compile(patternString, Pattern.DOTALL);
final Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
    System.out.println("Match found: " + matcher.group());
}
// => Match found: = ?abcdef;

See the Java demo and the regex demo. Regulex graph:

enter image description here

Notes:

  • while (matcher.find()) - iterates through all potential matches in the string
  • matcher.group() - accesses the match value.

Regex

  • (['"]?) - Capturing group 1: either ' or "
  • \? - a ? char
  • .*? - any 0+ chars, as few as possible (the pattern is compiled with Pattern.DOTALL, so it will match across line breaks, too)
  • ; - a semi-colon
  • \1 - the same value as captured in Group 1.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563