简体   繁体   中英

How to get only numbers at the beginning and end of a character in string with Regex?

Example String : "as8d79asf5.dfdg dg s dgd2011/25klsdsj jdıo84823 jhgfkasfsf 2001/26llkasdjfıo";

I want to get only number from string.

Result:

  1. 2011/25
  2. 2001/26

EDIT:

The number of characters is not clear in string. For Example : 2/1234 , 214/1 , 22/2545 . May be all.

How do I get this result ?

This should do the trick:

\d+/\d+

Otherwise if you exactly look for a 4digit/2digit:

\d{4}/\d{2}

仅使用@"\\d+/\\d+"就足够了

You could use an expression like (C# quoted):

@"\d{4}/\d\d"

Altho that has nothing to do with "beginning and end of character".

Try the following.

string input="as8d79asf5.dfdg dg s dgd2011/25klsdsj jdıo84823 jhgfkasfsf 2001/26llkasdjfıo";
        Regex nmRegex = new Regex(@"([\d]+/[\d]+)");
        var matches = nmRegex.Matches(input);
        List<string> matchedStrings = new List<string>();

        for (int i = 0; i < matches.Count; i++)
        {
            var g = matches[i].Groups;
            if (g.Count > 0)
                matchedStrings.Add(g[1].Value);
        }

Something like this regular expression maybe?

([[0-9]{4}\/[0-9]{2})

Not solid in C# though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM