Skip to content

Regular Expressions

Regular Expressions discussions

This category can be followed from the open social web via the handle regular-expressions@forum.codeproject.com

188 Topics 829 Posts
  • [Solved]

    regex help tutorial question learning
    24
    0 Votes
    24 Posts
    76 Views
    H
    Hello, I use VB.net for this one... (BTW) i know this isn't the thing you're looking for, but i hope i could at least help you... ;) Public Function ListItems As List(Of Item) Dim Result As New List(Of Item) Dim items As String = "asd=w2342 asd=12333" Dim parts() As String = Split(items, " ") 'First off i'm gonna split the items devided by spaces For Each part As String In parts Dim items() As String = Split(part, "=") 'Second i'm going to split the part into two seperate values Dim Value1 As String Dim Value2 As String Value1 = items(0) Value2 = items(1) Result.Add(New Item(Value1, Value2) 'Then you can assign these value to a class or something else you want to use this data in... Next Return Result End Sub I hope i could help you a bit in finding an answer to your problem... With kind regards, Niels Koomans
  • 0 Votes
    2 Posts
    6 Views
    M
    It isn't clear what the form of your input to the Regex is. What you show as "Line" is actually 4 lines (ignoring the blank lines). So is all of this really in a single string? I.e. string Line = "Ques xx dd\nStack #1:\nTask id #330\nTaskRecord{ccsssew fs sfsdf sdfsdf}"; Or are the lines separate strings in an array or list, or are they being read in line-at-a-time? In any case, the Regex you have is not correct. The } needs to be before the end of the line! For the ^ and $ to match beginning and end of line (not beginning and end of the whole string) you need to use Multiline mode. But to have . also match the \n (end of line) you need to use Singleline mode. I suppose having both modes set can work (I haven't tried it), but it will sure look strange in the code!!! So, if your input is separate strings that you are "processing" sequentially, you should do something like: bool collecting = false; List<string> collected = new List<string>(); foreach (string line in source-of-text-line-by-line) { if (!collecting && line.StartsWith("Stack")) { collecting = true; collected.Clear(); } if (collecting) { collected.Add(line); if (line.EndsWith("}")) { collecting = false; // Here do whatever processing you want with this set of collected lines } } }
  • MATLAB Code

    3
    0 Votes
    3 Posts
    9 Views
    Kornfeld Eliyahu PeterK
    I can't see how you can't see the connection... :laugh: I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
  • Me again - Denying certain characters, except they are escaped

    regex question
    1
    0 Votes
    1 Posts
    3 Views
    No one has replied
  • Parsing ASCII-Only string

    regex json question
    7
    0 Votes
    7 Posts
    16 Views
    M
    Gotcha! Thank you very much :thumbsup: Clean-up crew needed, grammar spill... - Nagy Vilmos
  • Parsing a string with match groups

    regex database json help tutorial
    3
    0 Votes
    3 Posts
    6 Views
    G
    You can use the alternation syntax "(a|b)". A match in your case is an expression in form "parameter=value", or an operator. Operator can be "or" or "and" and must have a space before and after. Following pattern produces the results you wanted. For an input string "Name=John or Name=Jane and Date=Now" you get matches "Name=John", " or ", "Name=Jane", " and ", "Date=Now": "(\w+=\w+|\s(or|and)\s)" If you want to use regex for validation only, you can do it this way (note that you get only a single match with this pattern): Regex.IsMatch(inputStr, "^(\w+=\w+|\s(or|and)\s)+$"); And you can go even further with validation. Following pattern uses positive look ahead/behind syntax to ensure that operators are enclosed with valid expressions: Regex.IsMatch(inputStr, "^(\w+=\w+|(?<=\w+=\w+)\s(or|and)\s(?=\w+=\w+))+$"); Gabriel Szabo
  • // THE WALKING DEAD SEASON 4 EPISODE 3 !!PUTLOCKER !!

    announcement
    1
    0 Votes
    1 Posts
    3 Views
    No one has replied
  • Matching a string.

    regex question
    7
    0 Votes
    7 Posts
    22 Views
    S
    Just to complete things, I also wanted to pick up the other bit of text at the other end. That is, given: yapyapyap ["fred"] moreyapyap I want to get the closing double quote through the closing square bracket whether there's white space between them or not. I did that using alternation as in this regular expression: ("]|"\s*]) It probably doesn't teach anyone anything they don't know already but I thought it would be worth a mention if only to complete what I need to do. If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
  • Replacing a string that a pattern matches.

    regex tutorial question csharp help
    1
    0 Votes
    1 Posts
    4 Views
    No one has replied
  • Help wanted with regex format.

    regex help question
    15
    0 Votes
    15 Posts
    33 Views
    E
    Programming is one industry where everyone is constantly attempting to specialize in generalizing!!!!
  • Regex Problem With Control Characters

    regex help tutorial question
    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • 0 Votes
    7 Posts
    17 Views
    L
    Maybe it checks if you have to go to work too. AFAIK 1.1 is work-free. Greetings - Jacek
  • hello

    com help question
    1
    0 Votes
    1 Posts
    3 Views
    No one has replied
  • Regular Expression to allow combination of character with digits only

    regex
    5
    0 Votes
    5 Posts
    11 Views
    Richard DeemingR
    Assuming you're using the .NET RegEx class, you can use a zero-width positive lookahead assertion[^]: ^(?=.*?\d)(?=.*?\D)\w{2,}$ This means: You're matching the entire string - ^ and $ The string must consist of at least two alpha-numeric characters - \w{2,} The string must contain any number of characters followed by a digit - (?=.*?\d) The string must contain any number of characters followed by a character that is not a digit - (?=.*?\D) "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
  • Advanced practice on regexp

    regex tutorial
    3
    0 Votes
    3 Posts
    10 Views
    M
    Thanks Peter. Any suggestions for regexp advanced exercises? Peter_in_2780 wrote: There are many cases where regex is not the appropriate tool. For example? I would depend on the case, I suppose. Cheers, Manuel
  • how to make a white space regex

    regex tutorial
    4
    0 Votes
    4 Posts
    13 Views
    J
    He tried to re-define your problem so that it can be easily translated to regular expressions: Quote: [beginning of string][whitespace] OR [whitespace](two or more) OR [whitespace][end of string] He wanted you to write it out yourself. However, I will guide you to the solution. Using Peter's suggestion, you will get: ^\s OR \s{2,} OR \s$ In Perl, this may look like: if ($str =~ /^\s/ || $str =~ /\s{2,}/ || $str =~ /\s$/) { print "String '$str' contains invalid white space sequence(s)."; }
  • part of string before a match

    regex tutorial question
    4
    0 Votes
    4 Posts
    12 Views
    M
    Also look at the various overloads of Regex.Split(), especially if the initial "match" is more than just a simple string.
  • Regex getting special caracter

    regex question
    3
    0 Votes
    3 Posts
    8 Views
    P
    Thanks you get the point.
  • how to make username regex for windows forms

    winforms regex tutorial question
    13
    0 Votes
    13 Posts
    36 Views
    P
    Same negative trick works, doesn't it? (?!\.[^.]*\.)(?!\-[^-]*\-)(?!\_[^_]*\_) Or (?![.-_][^.-_]*[.-_]) if he wants at most one of any of the three. Cheers, Peter Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
  • Extract tag within pre tag

    html regex xml help tutorial
    6
    0 Votes
    6 Posts
    14 Views
    P
    Sometimes it makes sense to split the problem up. Rather than attempting to grab the pre tags that contain a tags, why not grab the pre tags and then iterate over them - and do a second regex for the a tag. That way, you have the full context without worrying about backtracking. *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington "Mind bleach! Send me mind bleach!" - Nagy Vilmos CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier