-2

The Regular expression \"([^\"]*)\" is matching the string ""abcd"" as "" "" . What changes(I mean adding escape characters to the string) should be done to the string such that regular expression should match with ""abcd""

Sorry if it is an invalid question. But my requirement is to match the string with the given regular expression Thanks in Advance.

VSanka
  • 85
  • 2
  • 9
  • Try using a tool like http://regexr.com/ to isolate the problem. – AJ X. Nov 10 '16 at 13:42
  • @LupuSilviu that part of the regex matches anything **except** quotes. – UnholySheep Nov 10 '16 at 13:45
  • @UnholySheep you are correct. @VikranthSanka. you have to remove the double quotes from the string. `"abcd"` is a match. The Regex requires the string to start and end with quotes, and stops as soon as start and end quotes are found. – Lupu Silviu Nov 10 '16 at 13:47

1 Answers1

0

What changes should be done to the string such that regular expression should match with ""abcd""?

You can make regex match ""abcd"" by adding another \" from both sides to your code, and remove () (optional), no need for them. Your code will be:

\"\"[^\"]*\"\"

Demo: https://regex101.com/r/6sPoEc/6

If you want to limit what is inside the quotations to be only alphabetical, You can use [a-zA-Z].

\"\"[a-zA-Z]+\"\"

Demo: https://regex101.com/r/6sPoEc/5

If however you want to include alphabetics, numbers and underscore _ inside the quotation marks, use \w

\"\"[\w]+\"\"

Demo: https://regex101.com/r/6sPoEc/4

By the way, be careful with + and *.

+ can return at least one character or more while * can return zero or more characters which means that * can return empty quotations """"

Ibrahim
  • 6,006
  • 3
  • 39
  • 50