| |

VerySource

 Forgot password?
 Register
Search
View: 887|Reply: 7

Find a regular expression pattern string

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-13 13:30:01
| Show all posts |Read mode
Given an input string, use a regular expression to search for results between the first occurrence of a and the first occurrence of b.
The marks a and b can be arbitrary strings.
For example, abc <symbol> def </ symbol> ghi <symbol> jkl </ symbol>
How to get the def between <symbol> and </ symbol>?
Thanks in advance!
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-11 18:45:01
| Show all posts
My idea is: first find <symbol> def </ symbol>, then replace <symbol> and </ symbol>.
Reply

Use magic Report

0

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-13 20:15:01
| Show all posts
Among the conditions given by the landlord, some content needs to be clarified, that is, a and b, although a and b can be changed, they must meet certain conditions, for example, it appears in pairs or follows a certain format. You can use regular to match, can not be any string, otherwise you can not determine which is a and b you want, for example, a and b are as follows
<a> and </a>
<font> and </ font>
In this way, as well as the examples you give, you can use the following regular to extract the content you want

String yourStr = textBox1.Text;
Match m = Regex.Match (yourStr, "<(. *)> (. *?) </\\1>");
textBox2.Text = m.Groups [2] .Value; //m.Groups [2] .Value is the content to be extracted

This is just an example, you also need to specify the qualifications of a and b to give you the regularity you really need
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-5-9 16:15:02
| Show all posts
As the upstairs said, the requirements are relatively vague. Do you want to try this?
<[^>] *> ([^ <] *) </ [^>] *>

using System.Text.RegularExpressions;

// Regular expression object
Regex re = new Regex (@ "<[^>] *> ([^ <] *) </ [^>] *>");

// Match object
Match m = re.Match ("your string");

// found
if (m.Success)
{
    // turn up
}
else
{
    // Not found
}
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-6-17 02:15:01
| Show all posts
string yourStr = ......;
MatchCollection mc = Regex.Matches(yourStr, "<(symbol>)(?<content>.+?)</\\1", RegexOptions.IgnoreCase);
foreach(Match m in mc)
{
    m.Groups["content"].Value;//
}
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-6-23 09:00:01
| Show all posts
Modify upstairs:

string yourStr = @"abc<symbol>def</symbol>ghi<symbol>jkl</symbol>";
MatchCollection mc = Regex.Matches(yourStr, @"<symbol>(?<content>\w+)</symbol>");
foreach(Match m in mc)
{
    string _resoue = m.Groups["content"].Value;
}
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-7-8 12:45:01
| Show all posts
懒懒鱼


   Modify upstairs:

string yourStr = @"abc<symbol>def</symbol>ghi<symbol>jkl</symbol>";
MatchCollection mc = Regex.Matches(yourStr, @"<symbol>(?<content>\w+)</symbol>");
foreach(Match m in mc)
{
    string _resoue = m.Groups["content"].Value;
}

  
================================================== =================
You have a problem. Regular expressions default to greedy matching. So your match result is "def</symbol>ghi<symbol>jkl".
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-10 20:30:01
| Show all posts
Try this (?<=<\w+>).*?(?=<\\\w+>)
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list