none
RegEx question RRS feed

  • Question

  • Hi,

    I have a RegEx question. I don't know if it is possible with RegEx, here it goes :

    I have the following string :

    100010941 / 31.07.2015 

    I need to match or capture the numbers from the first part , so before the "/" and the leading "1" and following "0" should be left out of it.

    The result needed : 10941

    Other examples :

    1000093943 / 12.12.2015   Result would be : 93943

    1001932 / 01.12.2015 Result would be : 1932

    I have the following RegEx the find the specific string :

    \d.*\s\/\s(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d

    But I also need a RegEx to capture what is described above.

    My attempt :

    ([^0-1].*)[1-9][0-9].*

    Any ideas ?


    Mas Unit

    Thursday, August 27, 2015 6:33 AM

Answers

  • Without Groups, the expression is more complicated:

    Dim Input = "1000100414413 / 31.07.2015"
    
    Dim m = Regex.Match(Input, "(?<=^10+)([^0]\d+)(?=\s)")
    If m.Success Then
        Console.WriteLine(m.Value) ' prints "100414413"
    End If


    • Edited by Viorel_MVP Thursday, August 27, 2015 2:57 PM
    • Marked as answer by M in M Friday, August 28, 2015 8:44 AM
    Thursday, August 27, 2015 2:57 PM

All replies

  • Hi,

    Try this,

    //Input string.
                string input = "100010941 / 31.07.2015", inp2;
                //trim first the initial leading 1's and 0's before the Regex initiation.
                inp2 = input.TrimStart('1');
                Match m = Regex.Match(inp2.TrimStart('0'), @"\d+");

    Hope it helps.


    Thursday, August 27, 2015 7:10 AM
  • Thanks for the reply.

    That would work but it should be all regEx changing code is not an option.

    Kr


    Mas Unit

    Thursday, August 27, 2015 7:25 AM
  • Hi,

    How about this one:

    string input = "1000093943 / 12.12.2015";
                Match m = Regex.Match(Regex.Replace(input, @"^10*", ""), @"\d+");
                Console.WriteLine(m.Value);
    Thursday, August 27, 2015 7:38 AM
  • Not an option.

    To clarify more , this is what I have to solve :

    inputstring = "1000093943 / 12.12.2015" , comes form external  source

    regex1 is defined outside the code.

    Regex.Match(inputstring ,regex1 ) => result match : 93943

    I need to be able to define this without the need to change the code.

    Kr


    Mas Unit

    Thursday, August 27, 2015 7:51 AM
  • Try the next adjustment of the Regular Expression:

        10+(\d+)\s\/\s(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d

    When it matches, extract the first group.

    But if the string is already extracted by your first expression, then it is possible to write a simplified one to extract just the number, such as

        ^10+(\d+)\s

    • Edited by Viorel_MVP Thursday, August 27, 2015 10:29 AM
    Thursday, August 27, 2015 10:27 AM
  • Hi,

    Thanks.

    But still not correct because it will not work for the following input :

    1000100414413 => result should be 100414413 but is with your RegEx : 414413

    see first post for more examples

    Kr 


    Mas Unit

    Thursday, August 27, 2015 10:55 AM
  • It is not clear which one is incorrect, the long or the short one. This is an example with the second expression, supposing that the string was already validated by old code:

    string input = "1000100414413 / 31.07.2015";

    var m = Regex.Match( input, @"^10+(\d+)\s" );

    if( m.Success )

    {

           Console.WriteLine( m.Groups[1].Value ); // prints "100414413"

    }

     

    Thursday, August 27, 2015 11:13 AM
  • Strange.

    I exectuded this in VB.NET and didn't get the result you pointed out in your example :

    My output was : 1000100414413

     Dim input As String = "1000100414413 / 31.07.2015"
    
     If (Regex.Match(input, "^10+(\d+)\s").Success) Then
      Dim sStr = Regex.Match(input, "^10+(\d+)\s").Groups(0).Value
     End If

    Kr


    Mas Unit

    Thursday, August 27, 2015 11:44 AM
  • Try Groups(1).
    Thursday, August 27, 2015 11:47 AM
  • Ok. Thanks that actually works but requires me to change my code.

    currently I am using Regex.Match(....).Value

    Kr


    Mas Unit

    Thursday, August 27, 2015 12:19 PM
  • Without Groups, the expression is more complicated:

    Dim Input = "1000100414413 / 31.07.2015"
    
    Dim m = Regex.Match(Input, "(?<=^10+)([^0]\d+)(?=\s)")
    If m.Success Then
        Console.WriteLine(m.Value) ' prints "100414413"
    End If


    • Edited by Viorel_MVP Thursday, August 27, 2015 2:57 PM
    • Marked as answer by M in M Friday, August 28, 2015 8:44 AM
    Thursday, August 27, 2015 2:57 PM
  • You can use this RegEx code:

                string text = "1000093943 / 12.12.2015 ";
                var result = Regex.Match(text, "(10{1,5}).[^/]*");
                string output;
                if (result != null)
                    output = result.Groups[0].Value;

    You can change the RegEx first parenthesis to increase or decrease the number of 0 digits. Currently, it matches upto 5 zeros after digit 1

    "(10{1,5}).[^/]*"

    Thursday, August 27, 2015 4:09 PM
  • Thanks for the reply.

    This works !

    Unfortunately that specific RegEx causes a problem when I place it in my XML.

    It messes up my XML and I can't read it via XmlReader throws exception :

    Name can not start with the sign =, hexadecimale value 0x3D.

    <MatchContentRegEx>(?<=^10+)([^0]\d+)(?=\s)</MatchContentRegEx>
    

    Using "" doesn't help.


    Mas Unit

    Friday, August 28, 2015 8:38 AM
  • found it.

    &lt;

    Kr


    Mas Unit

    Friday, August 28, 2015 8:43 AM