switch text
-
Hello Friends
Lets say I have a document below with the following entries
Name: rushlan
account : 1234
user: abcd
Name: Tom
account: 5678
user: efghWhat i would like to do is replace account with user and vice versa. So my correct output will be :
Name: rushlan
account : abcd
user: 1234
Name: Tom
account: efgh
user: 5678Thank you
Rush -
Making the assumptions that there is no indentation, and that the extra space between
account
and:
is really there, and that you have windows CRLF newlines:- FIND =
(?-s)account : (.*)\r\nuser: (.*)$
- REPLACE =
account : $2\r\nuser: $1
- Search Mode = Regular expression
- Replace All
The first set of parens says to make sure . doesn’t match newline; the second the old account name and puts it into capture-group1, the third matches the old user name and stores it into capture-group2. Then the replacement puts capture-group2 with
account :
prefix and capture-group1 withuser:
prefix.----
Useful References
- FIND =
-
@Rushlan-Khan said in switch text:
What i would like to do is replace account with user and vice versa. So my correct output will be :
Since you did not include the examples inside of a “code box” (see the pinned post “Please Read This Before Posting” and the subsequent “Formatting Forum Posts”) your real data may not be exactly as you have portrayed in the example. Nonetheless I have made an assumption that the space between
account
and the:
was in error. This is a regular expression so search mode must be regular expression.Find What:
(?-s)(account:\x20*)(.+\R)(user:\x20*)(.+\R)
Replace With:${1}${4}${3}${2}
Terry
PS I see @PeterJones got there just before me. You will see we are both very similar, that the beauty of regular expressions. They can achieve the same results with different methods.
-
until Terry’s reply, I didn’t notice that one
account
had a space before the colon and the other didn’t. If I change my assumption toaccount
followed by 0 or 1 space followed by colon, and the replacement always needs to have 0 spaces betweenaccount
and colon, then it becomes:- Find What =
(?-s)account\h?: (.*)\r\nuser: (.*)$
- Replace with =
account: $2\r\nuser: $1
If you added extra groups to mine, like Terry used four groups, then you can preserve whether or not there’s a space in each
account :
- Find What =
(?-s)(account\h?: )(.*)\r\n(user: )(.*)$
- Replace with =
$1$4\r\n$3$2
Please learn from this that giving an accurate representation of the data is critical when asking such questions, because small variations can change the answer. The more effort you put into asking a clean question, the better the answers will be.
- Find What =
-
This post is deleted! -
Hello, @rushlan-khan, @peterjones, @terry-R and All,
Here is a new regex variant which does not care about anything before the ending values to invert :
SEARCH
(?xi) ^ ( account \W+ ) ( \d+ \R ) ( user \W+ ) ( \w+ \R )
REPLACE
\1\4\3\2
Best Regards,
guy038
-
Hi, @rushlan-khan, @peterjones, @terry-R and All,
I did not do it on purpose but my regex S/R allows splitting text, with a line-break, between the names of the fields and their corresponding values !
For instance, with this INPUT text :
Name: rushlan account : 1234 user : abcd Name: Tom account: 5678 user: efgh
and the regex S/R :
SEARCH
(?xi) ^ ( account \W+ ) ( \d+ \R ) ( user \W+ ) ( \w+ \R )
REPLACE
\1\4\3\2
it would correctly return this OUTPUT text :
Name: rushlan account : abcd user : 1234 Name: Tom account: efgh user: 5678
Why : just because the regex syntax
\W
, equivalent to[^\w]
, means any character which is not aword
char ! So this char may also be a line-break character (\r
or\n
)Best Regards,
guy038
-
@guy038 WOW!! thank you so much . Let me absorb this, I will get a back to you if I have any questions.
-
@Terry-R thanks for your help !! Sorry this is my first time here, I will read the rules for sure now.
-
@PeterJones Thanks for your help !