2

i have a string containing day name, month/day

Sun 6/9 - Sat 6/15

i need to change the sequence of date from above to

sun 9/6 - Sat 15/6

i can do this by writing code to split and then change sequence and join the result. but is there any minimal way to achieve this by using regex or vb.net code.

2 Answers 2

3

Description

This regex will find each date group and swap the digits.

regex: (\d{1,2})\/(\d{1,2})

replace with $2/$1

enter image description here

With input text:

Sun 6/9 - Sat 6/15

It yields

Sun 9/6 - Sat 15/6

Javascript Code Example:

<script type="text/javascript">
  var re = /(\d{1,2})\/(\d{1,2})/;
  var sourcestring = "source string to match with pattern";
  var replacementpattern = "$2/$1";
  var result = sourcestring.replace(re, replacementpattern);
  alert("result = " + result);
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

What tool did you use to make that very nice diagram?
@ Rune, I'm using debuggex.com. Although it doesn't support lookbehinds, named capture groups, or atomic groups it's still handy for understanding the expression flow. There is also regexper.com. They do a pretty good job too, but it's not real time as you're typing.
0

I dont know the regex syntax for vb.net but your search regex will be: ([a-zA-Z]{3}\s)(\d)/(\d) (three letters, followed by space followed by digit-slash-digit) and your replace regex will be: \1\3/\2. If you are familiar with the Unix sed command, it would be

sed -re `s|([a-zA-Z]{3}\s)(\d)/(\d)|\1\3/\2|g`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.