Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

'country-iso2'-Number as Reg expr.

Last post 11-05-2009, 5:30 PM by Aussie Susan. 3 replies.
Sort Posts: Previous Next
  •  11-05-2009, 8:25 AM 57162

    'country-iso2'-Number as Reg expr.

    Hi there,

    I logged on to ask for help. I need to check for following syntax:

    e.g.:

    • DE-78166
    • DE-77871
    • ...

    so always 2 characters, '-' and 4 or 5 digits.

    I need to convert it to

    • DE,78166
    • DE,77871
    • ...

    Thank you for your help,
    har

  •  11-05-2009, 10:06 AM 57163 in reply to 57162

    Re: 'country-iso2'-Number as Reg expr.

    Easy!
    Match this:
    \b(\w\w)-(\d{4,5})\b
    And replace it with this:
    $1,$2
  •  11-05-2009, 3:25 PM 57170 in reply to 57163

    Re: 'country-iso2'-Number as Reg expr.

    Hi!

    Thank you for your fast solution - but I still have problems I can´t solve...

    If I try

    myString = myString.replace(\b(\w\w)-(\d{4,5})\b, "$1,$2");

    Or also if I try 

    var re = new RegExp(document.demoMatch.regex.value, "g");

    myString = myString.replace(re, "$1,$2");

    It won´t work... What am I doing wrong???

    Thx again,

    har

  •  11-05-2009, 5:30 PM 57176 in reply to 57170

    Re: 'country-iso2'-Number as Reg expr.

    For a start, I'm not sure which programming language you are using but I'm guessing that it is one that does not understand regex expressions that only identified by their context as a function argument. Therefore, try presenting the pattern as a string:

    myString = myString.replace("\b(\w\w)-(\d{4,5})\b", "$1,$2");

    Some languages use a different replacement string syntax, so you might need to use something like "\1,\2" instead.

    Further, you may need to escape the back-slash characters, depending on how the language interprets string literals (c# allows the @"..." form to contain unescaped backslashes, PHP has similar forms of quoted string, PERL allows regex patterns when within certain syntax elements and so doesn't need the escape character escaped etc).

    Also, I would recommend that you read the posting guidelines in the sticky note at the beginning of this forum and present your question such that it contains the information we ask for there. The "document.demoMatch.regex.value" could contain anything - you really need to provide us with more information here. Finally, if you tell us something "won't work" then we really need to be told what it does so that it shouldn't and/or what it doesn't do that it should.

    Susan

View as RSS news feed in XML