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] C Code Parsing

    csharp visual-studio regex json help
    3
    0 Votes
    3 Posts
    8 Views
    Richard Andrew x64R
    Peter_in_2780 wrote: Expresso thinks you regex is OK. Thanks for your help. I solved it. It turns out to be a case of the computer doing what I told it to do instead of what I wanted it to do. The difficult we do right away... ...the impossible takes slightly longer.
  • Rename File Name Help

    help tutorial
    4
    0 Votes
    4 Posts
    12 Views
    B
    OK, that's different background. In that case, I'd rely on the "greedy" character of wild cards. (?<part1>.*)\s-\s(?<part2>.*)(?<ext>\.wmv) will catch the parts and the extension, and the re-order it to become part2 - part1.ext
  • Swap two values based on pattern

    regex tutorial question
    7
    0 Votes
    7 Posts
    21 Views
    P
    Good one. Now we all know a bit more about a different regex engine (Emacs/sed). Cheers, Peter Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
  • RegEx Problem

    regex help question
    5
    0 Votes
    5 Posts
    14 Views
    A
    Why did you edit your response? The code I see in the email notification I was sent for your reply to my message looks more correct than the code I now see. For one, you're going to need a for loop to iterate over the result of Matches (not Match). Match finds a single result, and Matches finds all results. For two, r.Match(WordToFind) is searching "WordToFind" rather than "Line". Kevin Marois wrote: once I get a match, how do I get the Key/Value data You can get groups based on the name. Something like this (I don't have my compiler open, so it may vary slightly): String key = m.Groups["Key"].Value; String value = m.Groups["Value"].Value; Kevin Marois wrote: RegexOptions.RightToLeft This may drastically change the functionality, but I'm not exactly sure what it does, so you may want to Google this. Kevin Marois wrote: I'm fairly frustrated at this point. This shouldn't be this difficult. Regular expressions are complicated, but powerful. You'll get used to them over time. From what others say, Expresso is a good tool to learn regular expressions, though I use a custom tool I built for myself. Thou mewling ill-breeding pignut!
  • can someone tell me whats wrong with this expression?

    regex csharp com xml help
    3
    0 Votes
    3 Posts
    12 Views
    P
    By default, regex matching is greedy. That is, wildcards will match the longest possible chunk of input, so you need to make your .* non-greedy. You do that by putting a ? after it, so your line becomes Regex blogsRegEx = new Regex(@"<entry><id>tag:myblog.*?</entry>"); If you are going to do ANYTHING nontrivial with regexes, get a copy of Expresso. (See our Free Tools forum for details.) Cheers, Peter Software rusts. Simon Stephenson, ca 1994.
  • Trying to match non-quoted sections

    database tutorial mysql sql-server sysadmin
    12
    0 Votes
    12 Posts
    32 Views
    A
    I was confused since I understood (say: assumed...) that you have an SQL script that you want to patch... Never assume anything ;-) In that case your initial regex is probably the simplest solution. Cheers Andi
  • Can anybody explain how this Regex works.

    csharp regex tutorial question
    8
    0 Votes
    8 Posts
    22 Views
    A
    See my explanation below (I know, this is very old topic, but I see it was not solved in this thread, so I added my lengthly explanation below). The Regex matches for spaces where the prefix expression ((?<=...)) matches. Far too complicated for cases where one wants to have a string split into part separated by spaces, ignoring spaces within "...". My preferred solution is using positive match criterion (as described in the sentence above): string pattern = @"\s*(""[^""]*""|\S+)\s*"; var fields = Regex.Matches(input, pattern).Cast<Match>().Select(m=>m.Groups[1].Value); Cheers Andi
  • How to regex this

    regex help tutorial
    8
    0 Votes
    8 Posts
    23 Views
    L
    The topic is pretty outdated -- I have done it manually for much less time than I had spent trying to build a proper regex ;P Thanks for a nice trick to ignore multiline settings, though. :thumbsup: Greetings - Jacek
  • Something I just learned, with IP addresses

    regex help question
    2
    0 Votes
    2 Posts
    8 Views
    A
    You might consider making it more readable, e.g.: string b = @"25[0-5]|2[0-4]\d|1?\d?\d"; // longest match first string n = @"(?:"+b+@")"; // wrap into non captuering group string w = @"(?:\*|"+b+@")"; // allow * and wrap into non captuering group string d = @"\."; // separating dot string ip = n+d+w+d+w+d+w; // the whole ip = b.(*|b).(*|b).(*|b) Cheers Andi
  • Either start or not a "d"

    question regex
    2
    0 Votes
    2 Posts
    8 Views
    B
    Solved it with @"\b(?d{1,2})\b"
  • Help with pattern

    regex javascript html help question
    2
    0 Votes
    2 Posts
    8 Views
    T
    Probably you need to take a look at this[^] But to pick the name, status values, you may try document.getElementById, Look at this Get Value of a cell of HTML Table[^] thatraja My Dad had a Heart Attack on this day so don't... Pompeyboy3 here | Nobody remains a virgin, Life screws everyone :sigh:
  • array of file extentions

    data-structures regex help
    4
    0 Votes
    4 Posts
    12 Views
    A
    Thank you :) [EOS] Help people,so poeple can help you.
  • Regular expression to check special character

    regex help question
    2
    0 Votes
    2 Posts
    6 Views
    M
    ^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[^a-zA-Z0-9]).{6,}$ ^ checks for the start of the input string. (?=.*[a-zA-Z]) checks that there are at least one letter. (?=.*[0-9]) checks that there are at least one digit. (?=.*[^a-zA-Z0-9]) checks that there are at least one special character (not a letter nor digit). .{6,} checks that there are at least 6 characters. $ checks for the end if the input string. See also XKCD: Password Strength -- MizardX (so)
  • Url rewriting rule

    com regex tutorial
    3
    0 Votes
    3 Posts
    12 Views
    A
    I am going to make a few assumptions. Since you said this is in IIS, you probably can't create .Net or JavaScript code to handle the redirect (i.e., all redirects must be done using some regexes in a configuration file). Also, I assume the number of underscores in the "mysite.com" URL is variable. I don't think you can do this with one redirect, but you can create several. For example, you can start off with these (create more to handle more underscores): ^http://www\.mysite\.com/manufacturers/(?<MAN>[a-z0-9]+)_[0-9]+/(?<PRO>[a-z9-9]+)_[0-9]+\.htm$ http://www\.newsite\.co\.in/free-msds-download/${MAN}/${PRO}/ ^http://www\.mysite\.com/manufacturers/(?<MAN1>[a-z0-9]+)_(?<MAN2>[a-z0-9]+)_[0-9]+/(?<PRO>[a-z0-9]+)_[0-9]+\.htm$ http://www\.newsite\.co\.in/free-msds-download/${MAN1}-${MAN2}/${PRO}/ ^http://www\.mysite\.com/manufacturers/(?<MAN>[a-z0-9]+)_[0-9]+/(?<PRO1>[a-z9-9]+)_(?<PRO2>[a-z9-9]+)_[0-9]+\.htm$ http://www\.newsite\.co\.in/free-msds-download/${MAN}/${PRO1}-${PRO2}/ ^http://www\.mysite\.com/manufacturers/(?<MAN1>[a-z0-9]+)_(?<MAN1>[a-z0-9]+)_[0-9]+/(?<PRO1>[a-z9-9]+)_(?<PRO2>[a-z9-9]+)_[0-9]+\.htm$ http://www\.newsite\.co\.in/free-msds-download/${MAN1}-${MAN2}/${PRO1}-${PRO2}/ That is simple enough, but that could end up being a lot of rules if you have a large range of underscores (e.g., "the_name_of_some_long_product" and "the_name_of_some_long_manufacturer"). Alternatively, you can redirect all of the "mysite" URL's that match the pattern you want to a single URL on "newsite" that takes the original URL as a query string. The page on newsite can then parse the query string and perform a redirect using C# or VB.Net code, which gives you more flexibility than a configuration file. Note: the above regexes are untested and may contain mistakes, but you get the idea. Somebody in an online forum wrote: INTJs never really joke. They make a point. The joke is just a gift wrapper.
  • 0 Votes
    7 Posts
    18 Views
    U
    Thank you for the sample and the link. It works.
  • Pattern

    regex help
    15
    0 Votes
    15 Posts
    39 Views
    S
    It would be better for us if you could come up with some examples ? As I understood from your given information, this is required to you: ^\D{5}\-\d{6}_\D{1,}$ So it should match with this kind of samples: aaaaa-666666_a
  • Regular Expression

    regex
    7
    0 Votes
    7 Posts
    20 Views
    S
    Have a look at this pattern: ^\d{3}[\s-]?\d{3}[\s-]?\d{4}$ modified on Tuesday, August 23, 2011 8:35 PM
  • Value between brackets with a sperator character [modified]

    regex help question
    3
    0 Votes
    3 Posts
    9 Views
    P
    What you saw is called 'greedy' matching. The better regex engines support non-greedy matching operators, which behave a bit more intuitively, even if they make the regex unreadable! Cheers, Peter Software rusts. Simon Stephenson, ca 1994.
  • Forming reguar expressions using a builder class?

    regex com tutorial question
    4
    0 Votes
    4 Posts
    10 Views
    P
    Naerling wrote: I was thinking of making something like that myself Well, go, man, go! It looks like a fun exercise. You could certainly start with just the parts you need now and add more as needed. You may need to leave out more "advanced" stuff like nested groups, but just encapsulating the most basic functionality may be of use to many.
  • Negative regular expression

    regex question tutorial learning
    5
    0 Votes
    5 Posts
    16 Views
    L
    If we didn't redesign our solution, this would probably be our course of action. So (although I won't use your solution ;-) ), I'm giving you a "5". Don't forget to rate answer, that helped you. It will allow other people find their answers faster.