Sohail Hameed
Sohail Hameed

Reputation: 988

Change sequence of text in string in ASP.net?

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.

Upvotes: 2

Views: 148

Answers (2)

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`

Upvotes: 0

Ro Yo Mi
Ro Yo Mi

Reputation: 15010

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>

Upvotes: 3

Related Questions