Regex to append some characters in a certain position
You may try the following find and replace, in regex mode:
Find: ^([^(]+)(.*?;)(CAT.*)$
Replace: $1$2$1;$3
The idea here is to just subdivide each line into pieces we need to thread together the replacement. In this case, the first capture group is the word which we plan on inserting after the first semicolon, before CAT
.
Demo
Just noticed you are using Python. We can try:
inp = """aarhus(iof>city>thing,equ>arhus);CAT(CATN),N(NP) ;
abadan(iof>city>thing);CAT(CATN),N(NP) ;
abandon(icl>leave>do,agt>person,obj>person);CAT(CATV),AUX(AVOIR),VAL1(GN) ;"""
output = re.sub(r'([^(]+)(.*?;)(CAT.*?;)\s*', '\\1\\2\\1;\\3\n', inp)
print(output)
This prints:
aarhus(iof>city>thing,equ>arhus);aarhus;CAT(CATN),N(NP) ;
abadan(iof>city>thing);abadan;CAT(CATN),N(NP) ;
abandon(icl>leave>do,agt>person,obj>person);abandon;CAT(CATV),AUX(AVOIR),VAL1(GN) ;
How do I add a character at a specific position in a string?
If you want to add a character after the sixth character, simple use the search
^(.{6})
and the replacement
$1,
(Example inserts a ,
)
technically spoken this will replace the first 6 characters of every line with MatchGroup 1 (backreference $1
) followed by a comma.
Regex to add a character at nth position?
You could use the ^
character to anchor the regular expression to the start of the string:
(?<=^.{5})
See regex tester
regex to match character in specific position
You say you're looking for C, F or E but looking for A in your example, so please include in the brackets any other letters you want to match, but what you're looking for is:
/^.{8}[CFE]/
It should be {8}
rather than {9}
because the way you had it, it'll match the first 9 characters and then match your letter in position 10.
java regex insert character as specific index position
Replace ^.{3}
(3 [{3}
] characters [.
] at the start of the string [^
]) with $&/
(the match $&
, followed by a /
).
Insert some characters in a specific place with a regex
Assuming there will always be no space between the close parentheses and the beginning of a new function (starting with an upper- or lower-case letter), you could use this. This also assumes that you will always want to insert a newline when there is a close parentheses immediately followed by a letter.
text <- "\\usage{afunction(arg = list())anotherfunction()}\\somethingelse{}"
gsub("(\\))([A-Za-z])", "\\1 \\\\n \\2", text)
# [1] "\\usage{afunction(arg = list()) \\n anotherfunction()}\\somethingelse{}"
How do I append String data to certain positions using python regular expressions?
You can back-reference matched group in re.sub
>>> import re
>>> s = "Some show 14x01 - the one where they do thing 1"
>>> re.sub(r"(\d{2}).(\d{2})", r"S\1E\2",s)
>>> 'Some show S14E01 - the one where they do thing 1'
Here \1
refer to first matched group and \2
refer to second matched group.
Related Topics
Cv2.Videocapture.Open() Always Returns False
How to Write a Lambda Function That Is Conditional on Two Variables (Columns) in Python
Running a Powershell Cmdlet in a Python Script
Conversion of String to Upper Case Without Inbuilt Methods
How to Use Installed Packages in Pycharm
What Is the Easiest Way to Convert Ndarray into Cv::Mat
How to Assign and Use Column Headers in Spark
How to Find the Closest Values in a Pandas Series to an Input Number
How to Determine If Current Time Is Within a Specified Range Using Python'S Datetime Module
How to Append Data Using Openpyxl Python to Excel File from a Specified Row
Python How to Remove Escape Characters from a String
Replacing Values in a Dataframe for Given Indices
Jsondecodeerror: Expecting Value: Line 1 Column 1 (Char 0)
Xlsxwriter: How to Open an Existing Worksheet in My Workbook
Get All the Diagonals in a Matrix/List of Lists in Python
Access Is Denied When Trying to Pip Install a Package on Windows