none
Help with right regular expression RRS feed

  • Question

  • Hello. I'm learning therefore i want to write in myself. I have a string like this

    +xz\a7ывмывмы---(4-95--)sda777-77d7++7+sa^%&ds-()ыпывпывпывп-

    i need get

    7(495)777-7777

    so i'd like first to get

    7(4

    after i'l get the rest. So i'm writing

    \d+.*\(\d+

    and i get this string

    7ывмывмы---(4

    so that's okay, now i want to remove group that i don't needed (i ask him to not save everything between 7 and ( ) so i'm writing

    \d+(?=.*)\(\d+

    but after this it write that there's no matches.

    How to write a correct regular to get 7(4 ?

    I hope i'l get the rest myself



    • Edited by PsilonRus Thursday, January 31, 2013 9:49 PM
    • Moved by Bob Shen Friday, February 1, 2013 7:42 AM
    Thursday, January 31, 2013 9:49 PM

Answers

  • source      +xz\a7ывмывмы---(4-95--)sda777-77d7++7+sa^%&ds-()ыпывпывпывп-

    regex        (\d+)(?:.*?)(\(\d+)

    testsite: http://regexlib.com/%28A%287tnGuevh6DhQCSbnuCDnDMFdPwZzmOTMcgvsAxNmliGC3iBuNrMKg_CrLtjKHU7XOZy-VM8PQeUbFc7lLsIDSr1x8nf2LoOxqE0_wiQRl86qA8OgYVi_TEYIIjAXB_mu8Kf-xmzhRp9Wlp6pVH1LwWD_MwoTbBqBgQHtKb4ej1x_lPZhM1rTJnduXEZYzN590%29%29/RETester.aspx

    result

    no matches.

    so in VS2012 same result

    Saturday, February 2, 2013 7:28 AM
  • Hi,

    And replace the excess ):

    (\))\)*

    Replace RE:

    $1

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by PsilonRus Monday, February 11, 2013 7:54 AM
    Monday, February 11, 2013 3:48 AM
    Moderator

All replies

  • I am not 100% clear on what you are asking.  I think you may be going for something like

    (\d).*\((\d).*(\d).*

    This should get you the 7(49 .  You should be able to figure out the rest of the pattern from there.

    Thursday, January 31, 2013 10:54 PM
  • It might be easier to just clear away the 'noise' characters, retaining only the digits as it appears you're sifting through characters looking for phone numbers (including nation code)

    consequently, if that IS what you're doing, the following might work ...

            private void Form1_Load(object sender, EventArgs e)
            {
    
                string phoneNoSample = "+xz\a7ывмывмы---(4-95--)sda777-77d7++7+sa^%&ds-()ыпывпывпывп-";
                string result = Regex.Replace(phoneNoSample, "\\D", "").Insert(7, "-").Insert(4, ")").Insert(1, "(");
    
                }
    
    

    Thursday, January 31, 2013 11:27 PM
  • in this case i can easily do not use regexp and write something like

    string str = "+xz\a7ывмывмы---(4-95--)sda777-77d7++7+sa^%&ds-()ыпывпывпывп-";
    str = new string(str.Where(Char.IsDigit).ToArray());
    Console.WriteLine("+" + str[0] + "(" + str.Substring(1,3) + ")" + " " + str.Substring(4, 3) + "-" + str.Substring(7));

    Friday, February 1, 2013 3:27 AM
  • i need a whole number like a match group, in worst case 3 groups

    1: 7

    2: 495

    3: 777-7777

    why it can't ignore thing that i ask to? I readed that (?= *SOMETH*) is the same as *SOMETH*, and difference is only that it won't save it in result group.

    for ex: example from msdn

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
       public static void Main()
       {
          string pattern = @"\b\w+(?=\sis\b)";
          string[] inputs = { "The dog is a Malamute.", 
                              "The island has beautiful birds.", 
                              "The pitch missed home plate.", 
                              "Sunday is a weekend day." };
    
          foreach (string input in inputs)
          {
             Match match = Regex.Match(input, pattern);
             if (match.Success)
                Console.WriteLine("'{0}' precedes 'is'.", match.Value);
             else
                Console.WriteLine("'{0}' does not match the pattern.", input); 
          }
       }
    }
    // The example displays the following output:
    //    'dog' precedes 'is'.
    //    'The island has beautiful birds.' does not match the pattern.
    //    'The pitch missed home plate.' does not match the pattern.
    //    'Sunday' precedes 'is'.
    "is" is used, but not saved. This is needed behaviour


    • Edited by PsilonRus Friday, February 1, 2013 3:53 AM
    Friday, February 1, 2013 3:32 AM
  • I think that you can exclude some fragments from the list of groups (using "?:"), but you cannot exclude them from the groups’ values. You can concatenate the groups manually, for example:

    string s = "+xz\a7ывмывмы---(4-95--)sda777-77d7++7+sa^%&ds-()ыпывпывпывп-";
    string p = @"(\d+)(?:.*?)(\(\d+)";
    Match m = Regex.Match(s, p);
    if (m.Success)
    {
        var r = string.Join(string.Empty, m.Groups.Cast<Group>().Skip(1).Select(g => g.Value));
        // r is "7(4"
    }


    • Edited by Viorel_MVP Friday, February 1, 2013 1:40 PM
    Friday, February 1, 2013 1:39 PM
  • source      +xz\a7ывмывмы---(4-95--)sda777-77d7++7+sa^%&ds-()ыпывпывпывп-

    regex        (\d+)(?:.*?)(\(\d+)

    testsite: http://regexlib.com/%28A%287tnGuevh6DhQCSbnuCDnDMFdPwZzmOTMcgvsAxNmliGC3iBuNrMKg_CrLtjKHU7XOZy-VM8PQeUbFc7lLsIDSr1x8nf2LoOxqE0_wiQRl86qA8OgYVi_TEYIIjAXB_mu8Kf-xmzhRp9Wlp6pVH1LwWD_MwoTbBqBgQHtKb4ej1x_lPZhM1rTJnduXEZYzN590%29%29/RETester.aspx

    result

    no matches.

    so in VS2012 same result

    Saturday, February 2, 2013 7:28 AM
  • Hi Psilonrus,

    Welcome to the MSDN Forum.

    >>no matches. so in VS2012 same result

    No, I test it on both this site and VS2012, they all return same result Viorel suggested.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 4, 2013 7:09 AM
    Moderator
  • That's fine, but it's only 1st step then i need get a whole number

    7(495)7777777

    so input can be like this

    1dgbdfxbv7dfgdfcbvbcvb(gsdgsdgsdg4idfjgdfg9kpokasfsd5fgsdfgsdg)gsdgsdg7fhgdfhdh7dfhdfhh7qwrqwr7poxckbvxc7qtionqwe7qpwoetjrqpo7

    Monday, February 4, 2013 7:17 AM
  • Hi PsilonRus,

    If your input is something like above input, you can just try 

    [\d\(\)]+

    And connect the matches to each other.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike FengModerator Thursday, February 7, 2013 12:32 PM
    • Unmarked as answer by PsilonRus Thursday, February 7, 2013 4:12 PM
    Monday, February 4, 2013 10:23 AM
    Moderator
  • input: 1dgbdfxbv7dfgdfcbvbcvb(gsdgsdgsdg4idfjgdfg9kpokasfsd5fgsdfgsdg)))gsd)gsdg7fhgdfhdh7dfhdfhh7qwrqwr7poxckbvxc7qtionqwe7qpwoetjrqpo7

    regex : [\d\(\)]+

    Match
    1
    7
    (
    4
    9
    5
    )))
    )
    7
    7
    7
    7
    7
    7

    7

    what is to be done next?


    Thursday, February 7, 2013 4:14 PM
  • Hi Psilonrus,

    Link them all together.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 8, 2013 4:40 AM
    Moderator
  • okay, i get

    17(495))))7777777

    so?

    Friday, February 8, 2013 9:15 AM
  • Hi,

    And replace the excess ):

    (\))\)*

    Replace RE:

    $1

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by PsilonRus Monday, February 11, 2013 7:54 AM
    Monday, February 11, 2013 3:48 AM
    Moderator