LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Capitilise characters using Regex

    Employee
    Posted 02-11-2015 04:50

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dstacey

    Hi,

    Is there a simple way of capitalizing certain letters using regexSubsitute?

    As an example if I create a string how can I capitilize the first letter and swap its position with the second character?

    e.g.
    x = "abcdef"
    y = regexSubstitute(x,"(.)(.)","$2$1",1) #swaps the letters over
    z = regexSubstitute(x,"(.)(.)","$2\u$1",1) #I thought using \u would work but it throws an invalid escape sequence error

    Any help anyone can give me on this is much appreciated - in general is there a document or graph that lists all the special character support and/or some more examples?

    Thanks
    Dave


  • 2.  RE: Capitilise characters using Regex

    Employee
    Posted 02-11-2015 07:15

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: andycooper

    Hi Dave,

    I've attached a cheat sheet I have for RegEx and below are some useful links: -

    regular-expressions-cheat-sheet-v1.pdf

    http://pcre.org/

    https://www.cs.tut.fi/~jkorpela/perl/regexp.html

    If you need any more details please let me know.

    Ta

    Andy


  • 3.  RE: Capitilise characters using Regex

    Employee
    Posted 02-11-2015 07:59

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: dstacey

    Hi Andy,

    Thanks the cheat sheets will help me a lot. I still couldn't see how to capitalize a letter in my substitution though so if anyone knows how to do that it would be very helpful (\u and \l seem to be valid in Perl but not in Brainscript)


  • 4.  RE: Capitilise characters using Regex

    Employee
    Posted 02-11-2015 15:51

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: mlam

    Hi dstacey,

    I've attached a couple of examples and I hope they help:
    • Dummy input 1 - I've used your example input here. In the filter node, I use the "toUpper" function to set the first letter to uppercase prior to using regex to swap the characters
    • Dummy input 2 - This is an example where I may have multiple cases in my input where I want to make a replacement. I use the regexMatchI function to find matches and then loop through to substitute. I've got two examples here that effectively do the same thing - the first one sets characters to capitals within a list via the "map" function and the second uses the "toUpper" function from above.

    Example graph: regexexample.brg

    In addition to the links Andy has provided above, I also find the Wikipedia page to be quite useful!: http://en.wikipedia.org/wiki/Regular_expression