NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Interesting, i get matches with brackets. Why? //my matches: {name}, {[email protected]} –
Georg
Jun 13 '18 at 7:35
2

@Georg Changed the answer accordingly. Included a group to display the value either with or without brackets. The difference to the other solutions is that Regex looks for matches, whereas the other answers are directly splitting the string. –
Fabian Bigler
Jun 13 '18 at 8:36



8


1
I never used regex before. I was abel to see similar questions in forum but not exactly what im looking for

I have a string like following. need to get the values between curly braces

Ex: "{name}{[email protected]}"

And i Need to get the following splitted strings.

name and [email protected]

I tried the following and it gives me back the same string.

string s = "{name}{[email protected]}";
string pattern = "({})";
string[] result = Regex.Split(s, pattern);
c#
regex
string
c#-4.0
string-split
Share
Edit
Follow
Flag
edited Jun 29 '13 at 12:56
asked Jun 29 '13 at 10:57

Kurubaran
8,21655 gold badges3737 silver badges6262 bronze badges
1

This is quite a good tool for creating regex patterns/learning regex gskinner.com/RegExr –
Chris Clayton
Jun 29 '13 at 11:20
Add a comment
Start a bounty
3 Answers

36

Use Matches of Regex rather than Split to accomplish this easily:

string input = "{name}{[email protected]}";
var regex = new Regex("{(.*?)}");
var matches = regex.Matches(input);
foreach (Match match in matches) //you can loop through your matches like this
{
var valueWithoutBrackets = match.Groups[1].Value; // name, [email protected]
var valueWithBrackets = match.Value; // {name}, {[email protected]}
}
Share
Edit
Follow
Flag
edited Jun 13 '18 at 8:35
answered Jun 29 '13 at 11:29

Fabian Bigler
9,41955 gold badges4040 silver badges6464 bronze badges

Interesting, i get matches with brackets. Why? //my matches: {name}, {[email protected]} –
Georg
Jun 13 '18 at 7:35
2

@Georg Changed the answer accordingly. Included a group to display the value either with or without brackets. The difference to the other solutions is that Regex looks for matches, whereas the other answers are directly splitting the string. –
Fabian Bigler
Jun 13 '18 at 8:36
Add a comment

16

Is using regex a must? In this particular example I would write:

s.Split(new char[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries)
Share
Edit
Follow
Flag
answered Jun 29 '13 at 11:24

Jan Dobkowski
59122 silver badges55 bronze badges

@FabianBigler I was under the impression that regex can easily achieve this, So i included Regex in the title :). I have changed the title now ;) –
Kurubaran
Jun 29 '13 at 12:59

Wouldn't the proposed solution also take as correct a string such as string s = "}name{[email protected]";? –
Josep
Aug 21 '14 at 15:17

I voted too fast… This solution does not provide an answer. Consider input string : {name}blabla{[email protected]}, you’ll have also “blabla” in the array… –
Jurion
Jan 8 '15 at 3:37
Add a comment

0

here you go

string s = "{name}{[email protected]}";
s = s.Substring(1, s.Length - 2);// remove first and last characters
string pattern = "}{";// split pattern "}{"
string[] result = Regex.Split(s, pattern);
or

string s = "{name}{[email protected]}";
s = s.TrimStart('{');
s = s.TrimEnd('}');
string pattern = "}{";
string[] result = Regex.Split(s, pattern);
Share
Edit
Follow
Flag
edited Jun 29 '13 at 11:32

Erik Schierboom
15.4k1010 gold badges6161 silver badges8181 bronze badges
answered Jun 29 '13 at 11:01

sangram parmar
7,89422 gold badges2121 silver badges4646 bronze badges

It works, but is there any way to get this result only using Regex pattern without using Substring ? –
Kurubaran
Jun 29 '13 at 11:05

im wondering if there is any was to to get the rsult only with regex pattern without doing any other string manipulation ? –
Kurubaran
Jun 29 '13 at 11:14

@Coder yes, take a look at my answer. ;) –
Fabian Bigler
Jun 29 '13 at 11:34
Add a comment
Your Answer


Community wiki
Not the answer you're looking for? Browse other questions tagged c# regex string c#-4.0 string-split or ask your own question.
The Overflow Blog
How to defend your attention and find a flow state
Keeping technologists in the flow state
Featured on Meta
We’ve made changes to our Terms of Service & Privacy Policy - January 2022
New post summary designs on site home pages and greatest hits now; everywhere...
Sunsetting Jobs & Developer Story
Hot Meta Posts
14
Merge [dexie] and [dexiejs] tags
15
Why was my flag for fixing a duplicate banner declined, even though it...
179
Top Questions re-design in home page
Linked
-1
Get all strings between two chars in a string
-3
Getting value using regex
1
Format string with dictionary in C#
Related
7122
What is the difference between String and string in C#?
1094
How to escape braces (curly brackets) in a format string in .NET
2306
How do I get a consistent byte representation of strings in C# without manually specifying an encoding?
2460
How do I get a substring of a string in Python?
2002
How can I print literal curly-brace characters in a string and also use .format on it?
219
Regular expression to get a string between two strings in Javascript
229
Regular Expression to get a string between parentheses in Javascript
3
Regex pattern to get string between curly braces
Hot Network Questions
What happens if we do rm -rf ./* in Linux? Can we restore the data?
What does "something you are" mean as a verification factor? Can you give some examples?
Highlight a Wordle guess
What is a healthy DFW rate?
How do Sola Scriptura defenders have a list of "scripture" since the list isn't mentioned in scripture?
Why are there three sets of wires for two switches, and how to wire up the neutral?
Can Solve[...] be used for set math? If so, how? If not, how else?
Why would my sect permanently change the color of my hair and irises?

1
I never used regex before. I was abel to see similar questions in forum but not exactly what im looking for

It works, but is there any way to get this result only using Regex pattern without using Substring ? –
Kurubaran
Jun 29 '13 at 11:05

im wondering if there is any was to to get the rsult only with regex pattern without doing any other string manipulation ? –
Kurubaran
Jun 29 '13 at 11:14

@Coder yes, take a look at my answer. ;) –
Fabian Bigler
Jun 29 '13 at 11:34

STACK OVERFLOW
Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness
PRODUCTS
Teams
Talent
Advertising
Enterprise
COMPANY
About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us
Cookie Settings
Cookie Policy
STACK EXCHANGE NETWORK
Technology
Culture & recreation
Life & arts
Science
Professional
Business
API
Data

Katra Ahluwalia: A Legacy of the Sikh-Misl Era

During the 18th Century, 12 Sikh-led sovereign states emerged in the Punjab region and they were known as Misls. They were one of the reasons for weakening of the Mughals during this era. Although there used to be disputes and territorial conflicts between them, but they presented a united front in case of any outside aggression.
They built a strong Sikh Confederacy, and were used to meet biannually to discuss legislative and other issues ailing the society and their kingdoms at the time.
Creation and Evolution of Katra Ahluwalia:
One of the states was Ahluwalia Misl, led by Misldar Sardar Jassa Singh Ahluwalia. He founded the Kapurthala State in 1772. Among his lesser-known contributions is the creation of Katra Ahluwalia— a well-planned and secured residential society built near the Golden Temple. Over time, the area transformed into a central commercial market. Historically, the Katra Ahluwalia was developed in three separate periods in the last two and a half centuries.

First Phase: Firstly, in 1760, Sardar Jassa Singh constructed the Qila (fortress) and also constructed a secured residential society within its four gates. Later, Misl Sardars used to encourage people to settle in the area by providing them with land free of cost. As a token amount, they only used to accept a tiny ball of jiggery (gur di rori) from the people in return of giving them the land. We know of it because it is recorded in the book ‘Tawarisk Sri Amritsar’, written by Giani Gian Singh (1822-1921).

But during invasion of Ahmed Shah Abdali and the resultant attack on Amritsar, many sections of the locality were destroyed. But the Misl Sardars swiftly responded. They recaptured the city, took possession of all the occupied shrines and residential areas. To strengthen city’s fortifications and for the security of historical localities within, they built several gates and military posts.
Second Phase: Maharaja Ranjit Singh wanted to diversify and strengthen Amritsar as a cultural and trading centre. So it is no surprise that during his reign, he sent for Marwari businessmen from Sikar, Rajasthan to settle and trade within the city. He was hoping to change the face of the city forever. Upon arrival, the Marwari traders were granted Katra Ahluwalia as a place to set up their businesses and residences as it was one of the more secure and prime location in the city.
Even now, the traders there trace their ancestry to Rajasthan. The unique architecture of some of the buildings here was a result of the work of many Irani or Iraqi masons who were asked to build houses in the city.

Third Phase: The British era saw the beginning of decline of Katra Ahluwalia. They were least inclined to preserve the culture there. In fact, they auctioned Qila Ahluwalia. It was bought by a businessman who later turned it into houses and shops. A major portion of traditional buildings was demolished and new ones now stood in their place. They were built on colonial architecture, with balconies and decorated shades.

Contemporary Status: The historical buildings here felt the burn of time, even more so than others because they were not actively preserved by the local authorities. Many are in dilapidated state. Today, only a small portion of the original fort remains and it is being used as a hotel. Lately, there has been a menace of non-approved hotels in the vicinity of the Golden Temple. Owners of those illegal establishments value profit over anything else, and therefore, no attention is being paid as to how our heritage is gradually disappearing.

But all is not lost. There are still buildings where visitors can witness old wooden facades and frescos with intricately decorated balconies. The central government took notice and began a restoration project under the scheme National Heritage City Development and Augmentation Yojana (HRIDAY). The project is still underway, with the aim of giving back to Amritsar the heritage that it lost.


I have a string like following. need to get the values between curly braces

Ex: "{name}{[email protected]}"

And i Need to get the following splitted strings.

name and [email protected]

I tried the following and it gives me back the same string.

string s = "{name}{[email protected]}";
string pattern = "({})";
string[] result = Regex.Split(s, pattern);



     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.