Hello-Help with regular expressions
-
Hello there, ive been messing around with regular expressions feature of search and replace and i have to say im not a programmer. I feel im asking a complex thing tho :)
Have a list in a txt file formatted as above
123:abc:1.2.3.4.5.2
def:456:6.7.8.9.6.3is it possible to make them like
1.2.3.4.5.2: 123:abc
6.7.8.9.6.3: def:456evidencing that what is before and after the first and second “:” can be words or numbers | what is among commas after second “:” (1.2.3.4.5) are only numbers.
I hope i’ve explained correctly what i intend to do :)
Thanks in advance! -
Not so complex, hopefully there is a learning opportunity for you here! Nothing is worse than someone that repeatedly posts about regular expressions, and demonstrates with the questions that they aren’t learning, but are rather just looking for someone to write their regular expression searches for them. :-D
Find what:
^([^:]+:[^:]+):([^\r\n]+)
Repl with:\2:\x20\1
Search mode: Regular expressionHere’s what it does:
Find the start of a line, followed by one or more characters that are not : (and remember that as group #1), followed by a :, followed by one or more characters that are not : (and remember that as group #2, followed by a :, followed by one or more characters out to the end of the line.
Replace the found text with the contents of group #2, a :, a blank space (the \x20) and the contents of group #1.
-
Hello, Antonio,
If you are sure that the last colon, of each line of your file, is the exact location which separates the two parts, of each line, to be reversed , an other formulation could be :
SEARCH :
(?-s)^(.+):(.+)
REPLACE :
\2: \1
( with a space between the colon:
and the back-reference\1
)Notes :
-
The
(?-s)
first part ensures that the dot special character (.
) represents a single standard character and not any End of Line character -
Then the part
(.+):
, look, from beginning of line (^
), for the longest non-empty range of standard characters, followed by the last colon of each line, which is stored as group 1, due to round brackets -
Finally, the part
(.+)
, grabs all the remaining standard characters of the current line ( = all text after the last colon character ) and stores them in group 2
Best Regards,
guy038
-