1

I want to know if I can highlight strings between {any string}

Here is what I have tried.

^{(.*)}$

This works, but this case retrieve the paranthesis also

{hi}

But the I want to retrieve only the string, not the parenthesis.

{hi}

Example:

hell(?=o) try this syntax, it will match exactly the hell word, when it is followed by "o" and not hello

Just like that, i want to match string, when it is started after open parenthesis and between close paranthesis and not the parenthesis.

Can anyone guide me, how can we ignore the Parenthesis and retrieve just the string?

As this query is involved in custom control, I need solution using Regex

Note

For more information see screenshots

Output

Output

Expected

Expected

Any help is appreciated.

Kannan
  • 191
  • 1
  • 1
  • 10
  • Please **clarify your specific problem or add additional details to highlight exactly what you need**. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question – Pedro Lobito Apr 30 '17 at 19:24
  • @PedroLobito updated my requirements. Please let me know, if it helps you – Kannan Apr 30 '17 at 19:32
  • You are asking about how to make an online regex tester work as expected. It's a wrong approach - use your regex inside code, and then ask a question. Note you already are using a capturing group - it is enough, just use `Match.Groups[1].Value` in C#. Or if you replace just use `$1`. Without your C# code, the question is unclear. – Wiktor Stribiżew Apr 30 '17 at 19:32
  • by highlight you mean `word` ? – Pedro Lobito Apr 30 '17 at 19:32
  • guys, i used online regex tester for better explanation. Example: hell(?=o) try this syntax, it will match exactly the hell word, when it is followed by "o". Just like that, i want to match string, when it is started after open parenthesis and between close paranthesis. – Kannan Apr 30 '17 at 19:35
  • @PedroLobito and wiktor: Do you guys understand my requirement? – Kannan Apr 30 '17 at 19:38
  • I only know that you want to highlight the regex match, but highlight how and where? using a `css` class, a html `tag`, like ``? – Pedro Lobito Apr 30 '17 at 19:41
  • @PedroLobito: no, see my above comments and i have updated my question clearly. Hope it helps. – Kannan Apr 30 '17 at 19:45
  • No, it didn't help me. GL – Pedro Lobito Apr 30 '17 at 19:56
  • I think the OP's specifically means 'highlight' as in successfully match on the value requested. Using the Regex tester that they're using the matches are 'highlighted' when matches are made. – Chris Cruz Apr 30 '17 at 20:00
  • And there is no need to - in C#, it is enough to use `var res = Regex.Matches(s, @"{(.*?)}").Cast().Select(m=>m.Groups[1].Value).ToList();` – Wiktor Stribiżew Apr 30 '17 at 20:04

4 Answers4

3

For whatever reason you cannot use groups - in your example you already use a lookahead. Just finish the pattern by also adding a lookbehind to the start of the pattern.

(?<={)[^}]*(?=})

[^}]* matches any amount of characters, that are not } if preceded by { if followed by }.

See demo at Regex Storm

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
2

You can use this regex:

(?<={)(.*)(?=})

See my example on regexstorm.net

Make sure to check this post about Lookahed and Lookbacks. Regex lookahead, lookbehind and atomic groups

Community
  • 1
  • 1
Michael Lopez
  • 153
  • 1
  • 9
1

Since it seems that you specifically want to retrieve the values between the curly braces (not parenthesis), you will need to perform the Regex match - that groups.

string phrase = "{hi} my name is Chris.";    
Regex exp = new Regex(@"\{(.*?)\}");
string match = exp.Match("{hi}").Groups[1].Value;

Now, I will admit that this isn't the most intuitive method; however, it certainly gets the job done. The Full Match returns '{hi}', whereas Group 1 returns the value you're looking for 'hi'.

Chris Cruz
  • 1,829
  • 13
  • 15
1

I want to know if I can highlight strings between {any string}

In the realm of regular expressions there is a difference between a match and a capture and basic grouping.

You want to match {hi} but you want to get hi into a capture Group. That is done using the ( ) construct pattern. A match can hold all three of those in different groups.

Keep these items in mind.

  • Groups[0] is always the whole match
  • Groups[1-N] are individual captures when ( ) construct is specified.
  • The pattern {Hi} will match "{Hi}" which can be found in the whole match of Group[0].
  • The pattern {(Hi)} will also match "{Hi}" in Group[0], but it will capture "hi" into Group[1].

I can't speak to the process of highlighting of text in winforms.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122