C # Regular expression replaces all content after the keyword is established

Source: Internet
Author: User

Title, replace all content after {outscipte} keyword with string. Empty (contains keywords)
How do you write this?
I am {outscipte} (. *) $ written, but nothing happens.

string str=regex.replace (" string ",@ "(? <=\{outscipte\}) [\s\s]*$ ","");

Good article on the 0 wide assertion well understood and explained


We use curly braces {} to specify how many times a single letter appears, such as W{3}, which indicates that W appears three times in a row. If it is a string of characters, how to complete it. We'll associate the precedence in the arithmetic notation, For example, the multiplication plus minus sign has a high priority. To make the minus sign first, you can enclose them in parentheses. It's also a string of characters to wrap around here.

For example (Arwen) {3} indicates that the character Arwen repeats three times. (ab[cde]*) {4} Indicates that ab[cde]* is repeated 4 times. In the case of a () after the inside is treated as a unit, a whole. Professional point is called grouping

In addition, it seems that a number can be used to refer to the preceding grouping. For example

String str = @ "123ABC";

String str = Regex.Replace (str,@ (\d+) \w+, @ "$1+456"); \ \ The result is 123+456.

(\d+) is a match of 123 and makes it a group, which is quoted by the group

Back to reference

Back to the reference, the name sounds awkward, in fact, the simple point is in the back of the match and used in the previous group . So what if you want to use that grouping again in a match somewhere back? Simple point of the simple approach to the group and then write down the words.

For example (Arwen) {2}123abc (Arwen) {3}. But this looks a bit redundant, so the whole lazy way out. Give each group a name and use the group name to represent that group. If you do not name directly, you will default by grouping from left to right in the order in which they appear. For example, the first occurrence of the above example (Arwen) is named 1, and then A (Arwen) named 2, if the back of a (James) named 3

After having a group name, the above example can be abbreviated as (Arwen) {2}abc\1{3} The format of the reference grouping is a slash \ Add group name

you can also explicitly give each group a name, in the form (? <name>exp) or (? '). Name ' exp ' where name is the group name, EXP is the content in the group . It's just an identifier. If I want to give the group (Arwen) The name of a, so write (? <a>arwen) such as the beginning of the example can be written as (Arwen) {2}\a{3}

In addition to the group default name or explicit name can also not give it the name format is (?: EXP) For example, the group (Arwen) changed to (?: Arwen) means that the group has no name. You can't refer to it by a name. What's the point of doing that? Because in addition to this here we see that the grouping name is useful, when you get the result by Regex.match (string source,string pattern), you can do some more detailed analysis, The group name will also be used. If you want to ignore the grouping information for a location, you can explicitly specify that the grouping does not have a name.

0 Wide Assertion

Some of the simple matches in front of us are the first to know some information about the sub-characters to match, and then to represent it with some meta-characters and restricted characters. This is a direct match. There are also some indirect matching information, such as knowing what character is in front of the substring, and what characters are followed by the hips.

So there are 0 wide assertions this strange noun, in fact, it means that the meaning is that, 0 wide means that the expression does not occupy a place, the width of 0, such as the front of the ^$ the beginning or end of the string. It does not occupy the place, Represents only the beginning or end of such a position concept. As for the assertion, it is a statement in the theory of logic. The simple saying is to make a judgment, to conclude.

0 Width Positive lookahead assertion (? =exp) It represents a string followed by a string exp such as. * (? =fool) indicates that a character ends with fool. If there are characters you are afool. Then the matching sub-character is a

0 Width is recalling post assertion (? <=exp) It indicates that a string preceded by an exp (? <=fuck) indicates a string preceded by a fuck. If there are characters fuck you match the result is you

There is a string to judge by either the front or the back, and nature can be judged by not having any characters in front or back. That's the opposite. The method is similar, just = number change!

0 Width Negative lookahead assertion (?! EXP) It means that a string is not followed by exp such as Hao (?!). \d) indicates that the character Hao is not followed by a number, and if there is a character hao123, it does not match. If the character Hao 123 matches to Hao

0 Width Negative review post assertion (? <!exp) It means that there is no exp in front of a string, such as (? <!\d) Hao means there is no number before the string Hao. If there is a character 123hao then the match fails. If the character 123 Hao matches to Hao

In fact, such as the zero-degree assertion that the term is too tangled, you simply do not care about him. Just think of the front, there, there is no, there is no such plain words in the back.

Greed and laziness

We know that because the matching conditions are often vague, the matching results can be different, such as having character Anrwen, and matching with a.*n. That an and Anrwen are all eligible results.

A match to an will no longer match down, nature is very lazy practice, so called lazy match . The exact meaning is to match the short characters as much as possible.

The opposite is the greedy match , which matches as many characters as possible.

The default practice is greedy matching. So the above example matches the result is Anrwen

If we want the result of an, write this a.*?n add a question mark

As for the case, it is natural that a qualifier appears with a character repeated several times.

Like * +?  {n} When these symbols appear, they can be used to become *?  +? {n}?

C # Regular expression replaces all content after the keyword is established

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.