## Substitute even or odd string matches

string odd or even java

how to print even and odd position characters of an array of strings in java

swap even and odd characters in a string python

write a code to print characters on even positions in python

count of strings that can be formed using a, b and c under given constraints

generate a string with characters that have odd counts

swap even and odd characters in a string - c

Straight off the bat: this is not just about matching numbers.

There are lots of `**`

scattered around **in a document**, and I need to replace them with `1`

and `2`

.

##### Sample input

**Lorem ipsum dolor **sit amet, consectetur adipisicing elit. ** Ad velit delectus ** quidem itaque eum **accusamus reprehenderit** illo culpa **** praesentium** ea fugit ****hic in vel officiis, expedita sit **** et harum enim quaerat, **** ab corporis quo atque perspiciatis. Minima odit obcaecati** ** reiciendis, sed rerum ** labore. In fuga, ** aspernatur earum aliquid ** ****** **commodi delectus?

##### Desired output

1Lorem ipsum dolor 2sit amet, consectetur adipisicing elit. 1 Ad velit delectus 2 quidem itaque eum 1accusamus reprehenderit2 illo culpa 12 praesentium1 ea fugit 21hic in vel officiis, expedita sit 21 et harum enim quaerat, 21 ab corporis quo atque perspiciatis. Minima odit obcaecati2 1 reiciendis, sed rerum 2 labore. In fuga, 1 aspernatur earum aliquid 2 121 2commodi delectus?

The only thing I got in mind is doing a loop where replaces the first occurrence found (`s/\*{2}/1/`

), then for the second (`s/\*{2}/2/`

), rinse and repeat, but since I still don't know how to translate this into shellscript, I was wondering if this is doable with a couple of piping `sed`

commands.

tr '\n' $'\x1' | sed 's/\*\*/\x2/g' | sed 's/\x2\([^\x2]*\)\x2/1\12/g; s/\x02/1/' | tr $'\x1' '\n'

- The first
`tr`

substitutes newline for an unreadable character equal to hex number`0x01`

. - Then the first
`sed`

substitutes two`**`

for hex`0x02`

. - Then any pattern
`0x02<anything>0x02`

is substituted for`1<anything>2`

- The last unamtched
`\x02`

is substituted by`1`

. - Then the
`0x01`

is replaced back by a newline.

Live version at tutorialspoint.

The substitution `*`

for `0x02`

is because, we can't do something along `sed 's/**\(^**\)**/...`

, ie. greedy match a string until multi-char pattern is found (or I don't know how to do it). So I just substitute two characters `**`

by one unredable character and handle it. That way i can handle single `*`

correctly, like `**Lor*em ip*sum**`

.

If you have GNU sed with `-z`

option, the substitution for the newline is not needed.

**Distinct strings with odd and even changes allowed,** It contains well written, well thought and well explained computer science and Two strings are distinct if on applying the following operations on one string the The encoded has counts of even and odd positioned characters separated by a Longest subsequence consisting of alternate vowels and consonants · Count For a number to be even, substring must end wit an even digit. We find all the even digits in the string and for each even digit, count the number of substrings ending with it. Now, observe that the number of substrings will be index of that even digit plus one.

Given:

$ cat file **Lorem ipsum dolor **sit amet, consectetur adipisicing elit. ** Ad velit delectus ** quidem itaque eum **accusamus reprehenderit** illo culpa **** praesentium** ea fugit ****hic in vel officiis, expedita sit **** et harum enim quaerat, **** ab corporis quo atque perspiciatis. Minima odit obcaecati** ** reiciendis, sed rerum ** labore. In fuga, ** aspernatur earum aliquid ** ****** **commodi delectus?

Your best bet is `perl`

or `awk`

:

$ perl -lpE 's/\*\*/$cnt++%2+1/ge' file

This uses perl's ability to eval an expression with the substitution.

Method:

- Increment a counter
`$cnt++`

to get get`(0,1,2,3,...)`

with each substitution in`s/\*\*/$cnt++%2+1/ge`

; - Take the modulo
`%2`

of that series to get`(0,1,0,1,0...)`

then add`1`

to get`(1,2,1,2...)`

for each alternating substitution.

By the same method, you can use `awk`

with a `while`

loop:

$ awk '{while (sub(/\*\*/,cnt++%2+1))}1' file

Alternatively, you can *slurp* the entire file (with `-0777`

) and then do a global replace for `**[stuff between maybe on multiline]**`

and replace with `1[stuff between maybe on multiline]2`

:

$ perl -0777 -lnE '$s=$_; $s=~s/\*\*([\s\S]*?)\*\*/1${1}2/g; END{$s=~s/\*\*/1/; say $s;}' file

Note the final `$s=~s/\*\*/1/;`

in case there are an odd total number of replacements.

All three cases, prints:

1Lorem ipsum dolor 2sit amet, consectetur adipisicing elit. 1 Ad velit delectus 2 quidem itaque eum 1accusamus reprehenderit2 illo culpa 12 praesentium1 ea fugit 21hic in vel officiis, expedita sit 21 et harum enim quaerat, 21 ab corporis quo atque perspiciatis. Minima odit obcaecati2 1 reiciendis, sed rerum 2 labore. In fuga, 1 aspernatur earum aliquid 2 121 2commodi delectus?

**A Textbook on ATM Telecommunications: Principles and Implementation,** They augment a basic bipolar code by replacing a string of N zeros with an N length code, which contains pulses which deliberately introduce bipolar violations. representing number of bipolar pulses (1s) since last substitution Odd Even replace every character at odd index with "&" in a string - replace.java

This might work for you (GNU sed):

sed -zE 's/(\*\*)([^*]*(\*[^*]+)*)\1/1\22/g' file

Slurp file into memory. Match `**...**`

and replace front `**`

by 1 and back `**`

by 2 globally throughout the file.

**How to get odd and even position characters from a string ,** function encrypt(text) { let removedText = ''; const replacedText1 = text.replace(/(.)(.)?/g, (_, firstChar, secondChar) => { // in case the match was at \$\begingroup\$ @Dream_Cap [even, odd] makes an array with 2 elements. [j & 1] selects an index from the array. j & 1 uses bitwise AND to get same effect as j % 2. Once one has been selected, it adds another character to it. But unless even and odd are already saved in an array, this doesn't work.

With GNU awk for multi-char RS and RT:

$ awk -v RS='[*][*]' '{ORS=(RT ? (ORS%2+1) : "")}1' file 1Lorem ipsum dolor 2sit amet, consectetur adipisicing elit. 1 Ad velit delectus 2 quidem itaque eum 1accusamus reprehenderit2 illo culpa 12 praesentium1 ea fugit 21hic in vel officiis, expedita sit 21 et harum enim quaerat, 21 ab corporis quo atque perspiciatis. Minima odit obcaecati2 1 reiciendis, sed rerum 2 labore. In fuga, 1 aspernatur earum aliquid 2 121 2commodi delectus?

**Competitions for Young Mathematicians: Perspectives from Five ,** Solution We will show that the execution of any of the admissible substitution does the parity of the number of the letter a in even (odd) positions in the string. 8.5.4 Other Invariants Problem 5.5 Each of 20 given cards contains one of the print(chars, 0, 2); to print the even indices, and setting the offset to 1, will print the odd ones. Of course you should add validation to the input parameters of this method, checking that the offset is within the bounds of the array etc. You could also replace the first for loop, with a foreach statemnet.

**Popular Lectures on Mathematical Logic,** Thus, if R contains such a part UyV, the part must occur in a finite initial string and The first alternative is impossible because no matter what R,. an even or odd position in Run, either KyKy or yKyK must be a string of ab 's and ba's which Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters.

**Regex to replace every odd and even occurence in string ,** So I'm struggling about regex on markdown exercise on exercism.io Basically how to turn something like this: "_hello_ kevin, please go to chair Approach: Split the string using split () function. Iterate in the words of a string using for loop. Calculate the length of the word using len () function. If the length is even, then print the word.

**Regular expression for strings with even number of a's and odd no ,** I would do as follows: regex even matches the symbol a, then a sequence of b's, odd)* even* ) Of course one can replace every occurence of even and odd in the A string with an even number of a's and an odd number of b's will satisfy this Part 1: Bounding the machine to read only one odd alphabet so the machine can read only a or b. Part 2: Bounding the machine to read many times even length alphabet like a machine can read aa or bb or ab or ba and machine can read it many times by repeating the loop(*).

##### Comments

`echo "** ** ** ** ** ** **" | sed -E 's/\*\* \*\*/1 2/g; s/\*\*/1/'`

- Sure the example is vague, but you're missing the point. I already mentioned that I have a
**document**with lots of couple of asterisks`**`

scattered around. I need to replace those. - Use
`cat file | perl -0pe 's/\*\*(.*?)\*\*/1${1}2/gs' | sed 's/\*\*/1/'`

- @Fabián - make it less vague, then. stackoverflow.com/help/how-to-ask
- @Cyrus added a better sample input and desired output
- You need to add a final substitution to get a final
`**`

if there are a odd total number. - @dawg it is unclear from the OP if an odd number of
`*`

's should be included in the previous`1...2`

or the start of the next/last in-between block. As is the solution caters for the test data provided.