Extract a String Between Double Quotes

Extract a string between double quotes

Provided there are no nested quotes:

re.findall(r'"([^"]*)"', inputString)

Demo:

>>> import re
>>> inputString = 'According to some, dreams express "profound aspects of personality" (Foulkes 184), though others disagree.'
>>> re.findall(r'"([^"]*)"', inputString)
['profound aspects of personality']

How to extract string between double quotes in java?

This code will replace all quotes with one space and save quotes without brackets in the list:

public static void main(String[] args) {    
String str = "According to some, dreams express \"profound aspects of personality\" (Foulkes 184), though \"others disagree\" but truth is.";
Pattern pattern = Pattern.compile("\".*?\"");
Matcher matcher = pattern.matcher(str);

List<String> quotes = new ArrayList<>();
StringBuffer buffer = new StringBuffer();

while (matcher.find()) {
String quote = matcher.group();
int length = quote.length();
quotes.add(quote.substring(1, length - 1));
matcher.appendReplacement(buffer, " ");
}
matcher.appendTail(buffer);

System.out.println(buffer.toString());
System.out.println(quotes);
}

This solution needs some additional fixes depends on existence of empty brackets in a text, but it work in your case.

Output:

According to some, dreams express (Foulkes 184), though but truth is.

[profound aspects of personality, others disagree]

How to Extract the string between double quotes having newline embedded in between the string?

You're using the wrong modifier on your regex. The /m treats the string has multiple lines where you actually want to use /s which changes "." to match any character including \n.

Changing this won't actually get you the output you want because you're repeatedly applying the transformation and it will delete any past quoted portions too. You want to also use the /g modifier which will find all possible matches and then only apply the regex the once.

use strict;

my $whole_text="\"Ankit Stackoverflow is \n awesome\" \"a\" asd asd \"he\nllo\"\n";

$whole_text =~ s/(.*?)"(.*?)"(.*?)/$2/sg;

And then if you want to get rid of the \n you'd also need.

$whole_text =~ s/\n//sg;

Regex - extract string between double quotes where line starts with a keyword

If you are using Javascript, and the positive lookbehind is supported (you don't need the multiline flag, only the global flag)

(?<=classes!\((?:"[^"]*",\s*)*")[^"]*(?=")
  • (?<= Positive lookbehind to assert to the left
    • classes!\( Match classes!(
    • (?:"[^"]*",\s*)* Match optional repetitions of "...",
    • " Match a double quote
  • ) Close lookbehind
  • [^"]* Match optional chars other than "
  • (?=") Assert " to the right

Regex demo

const regex = /(?<=classes!\((?:"[^"]*",\s*)*")[^"]*(?=")/g;
const str = `classes!("text-xl", "text-blue-500")
String::from("test")
classes!("text-sm")
`;
console.log(str.match(regex))

Extract text between quotation using regex python

If you want to extract some substring out of a string, you can go for re.search.

Demo:

import re

str_list = ['"abc"', '"ABC. XYZ"', '"1 - 2 - 3"']

for str in str_list:
search_str = re.search('"(.+?)"', str)
if search_str:
print(search_str.group(1))

Output:

abc
ABC. XYZ
1 - 2 - 3

How to extract a string from double quotes?

As long as the format stays the same you can do this using a regular expression. "([^"]+)" will match the pattern

  • Double-quote
  • At least one non-double-quote
  • Double-quote

The brackets around the [^"]+ means that that portion will be returned as a separate group.

<?php

$str = 'This is a text, "Your Balance left $0.10", End 0';

//forward slashes are the start and end delimeters
//third parameter is the array we want to fill with matches
if (preg_match('/"([^"]+)"/', $str, $m)) {
print $m[1];
} else {
//preg_match returns the number of matches found,
//so if here didn't match pattern
}

//output: Your Balance left $0.10

RegEx: Grabbing values between quotation marks

I've been using the following with great success:

(["'])(?:(?=(\\?))\2.)*?\1

It supports nested quotes as well.

For those who want a deeper explanation of how this works, here's an explanation from user ephemient:

([""']) match a quote; ((?=(\\?))\2.) if backslash exists, gobble it, and whether or not that happens, match a character; *? match many times (non-greedily, as to not eat the closing quote); \1 match the same quote that was use for opening.



Related Topics



Leave a reply



Submit