0

I am using search and replace in an advanced text editor that supports regex.

Now i want to replace ##,##- with -##,## (where ##,## stands for any number)

Any help what search and replace expressions I need to use to achieve this?

E.g. \d- will only replace the - itself.

3
  • 1
    Care to say which 'advanced text editor'? Commented Feb 8, 2015 at 20:59
  • Can you post some examples of lines before and after the replacement? ("Any number" is pretty vague. I assume you do want 1- changed to -1 and do not want one- changed to -one, but there are a lot of cases where I don't know whether you would want them changed.) Commented Feb 8, 2015 at 20:59
  • @ruakh sorry didnt mention I am searching a collumn, so I am only dealing with digits. ( I meant to say any number of digits) bad english - no native speaker ;) in any case cristobalitos solution did the trick Commented Feb 8, 2015 at 22:17

1 Answer 1

1

Not sure what the replace semantics are on your advanced text editor but you'll likely want to use grouping to do the replacement. Something like

(\d+)(,?)(\d+)-

will match the pattern and assuming something like $1 matches the groups (the bits between the brackets), then

-$1$2$3

should suffice where $2 matches the (I'm guessing optional) ','.

Sign up to request clarification or add additional context in comments.

1 Comment

Although your regex marks the , as optional, it requires at least one digit before it and at least one digit after it, with the effect that it requires at least two digits even if there is no comma. I doubt that is intentional. Also -- there's no need to have three separate capture groups. Replacing (\d+,?\d+)- with -$1 would have the same effect.

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.