R regular expression: isolate a string between quotes

RockScience

I have a string myFunction(arg1=\"hop\",arg2=TRUE). I want to isolate what is in between quotes (\"hop\" in this example)

I have tried so far with no success:

gsub(pattern="(myFunction)(\\({1}))(.*)(\\\"{1}.*\\\"{1})(.*)(\\){1})",replacement="//4",x="myFunction(arg1=\"hop\",arg2=TRUE)")

Any help by a regex guru would be welcome!

Avinash Raj

You could use regmatches function also. Sub or gsub only works for a particular input , for general case you must do grabing instead of removing.

> x <- "myFunction(arg1=\"hop\",arg2=TRUE)"
> regmatches(x, gregexpr('"[^"]*"', x))[[1]]
[1] "\"hop\""

To get only the text inside quotes then pass the result of above function to a gsub function which helps to remove the quotes.

> x <- "myFunction(arg1=\"hop\",arg2=TRUE)"
> gsub('"', '', regmatches(x, gregexpr('"([^"]*)"', x))[[1]])
[1] "hop"
> x <- "myFunction(arg1=\"hop\",arg2=\"TRUE\")"
> gsub('"', '', regmatches(x, gregexpr('"([^"]*)"', x))[[1]])
[1] "hop"  "TRUE"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular Expression, Match Between Single Quotes after Certain String

From Dev

Regular Expression to find values between quotes

From Dev

Regular expression to match anything between combination of quotes

From Dev

Regular expression to extract number with sign between quotes

From Dev

Regular Expression for a character not appearing between double quotes

From Dev

regular expression cut string between

From Dev

regular expression cut string between

From Dev

Regular expression for single quotes

From Dev

Bash regular expression with quotes

From Dev

how to select numbers that is not between double/single quotes with regular expression in javascript

From Dev

PHP - Regular Expression to Remove Single Quotes From Beginning and End of a String

From Dev

Python regular expression on single quoted string with escaped single quotes

From Dev

Regular expression to get a constant string wrapped with square brackets or quotes

From Dev

Python regular expression r prefix followed by three single (or double) quotes

From Java

Regular Expression to get a string between parentheses in Javascript

From Dev

regular expression get a string between two strings

From Dev

regular expression get a string between two strings

From Dev

regular expression for getting the content between [ ] in string in php

From Dev

Regular expression to get a sub string between {}

From Dev

Regular expression between a string and symbol Java

From Dev

the difference between `\\s|*` and `\\s|[*]` in regular expression in r?

From Dev

Regular expression to display >quotes in style

From Dev

R Regular expression for string containing full stops

From Dev

Capturing parts of string using regular expression in R

From Dev

Find regular expression string and replace it in R

From Dev

Regular expression for validating input string in R

From Dev

VB.Net - Use regular expression to identify new lines between double quotes

From Dev

difference between .* and * in regular Expression

From Dev

regular expression not between alphanumeric

Related Related

  1. 1

    Regular Expression, Match Between Single Quotes after Certain String

  2. 2

    Regular Expression to find values between quotes

  3. 3

    Regular expression to match anything between combination of quotes

  4. 4

    Regular expression to extract number with sign between quotes

  5. 5

    Regular Expression for a character not appearing between double quotes

  6. 6

    regular expression cut string between

  7. 7

    regular expression cut string between

  8. 8

    Regular expression for single quotes

  9. 9

    Bash regular expression with quotes

  10. 10

    how to select numbers that is not between double/single quotes with regular expression in javascript

  11. 11

    PHP - Regular Expression to Remove Single Quotes From Beginning and End of a String

  12. 12

    Python regular expression on single quoted string with escaped single quotes

  13. 13

    Regular expression to get a constant string wrapped with square brackets or quotes

  14. 14

    Python regular expression r prefix followed by three single (or double) quotes

  15. 15

    Regular Expression to get a string between parentheses in Javascript

  16. 16

    regular expression get a string between two strings

  17. 17

    regular expression get a string between two strings

  18. 18

    regular expression for getting the content between [ ] in string in php

  19. 19

    Regular expression to get a sub string between {}

  20. 20

    Regular expression between a string and symbol Java

  21. 21

    the difference between `\\s|*` and `\\s|[*]` in regular expression in r?

  22. 22

    Regular expression to display >quotes in style

  23. 23

    R Regular expression for string containing full stops

  24. 24

    Capturing parts of string using regular expression in R

  25. 25

    Find regular expression string and replace it in R

  26. 26

    Regular expression for validating input string in R

  27. 27

    VB.Net - Use regular expression to identify new lines between double quotes

  28. 28

    difference between .* and * in regular Expression

  29. 29

    regular expression not between alphanumeric

HotTag

Archive