Python convert to integer or string [duplicate] - python-2.7

This question already has answers here:
How to extract integers from a string separated by spaces in Python 2.7?
(4 answers)
Closed 5 years ago.
I have a string like below,
string str ="3rd Floor, aaa Mall, bbb, cccc, dd, ee, 123456 ,ff"
I have to extract "123456" from above string.
I am using below code to extract ,
p = re.findall(r'\b\d{6}\b',value)
and output is
[u'123456']
I have tried int(p) to get output as 123456 but its not working.

Try:
ints = []
for s in p:
ints.append(int(s))
Then use ints as you like.
UPDATE
If you are sure there will be one and only one integer, then do:
this_is_an_int = int(p[0])

Related

Remove Bracket [*TESTABC*] along with content from String prefix in java [duplicate]

This question already has answers here:
Java replace all square brackets in a string
(8 answers)
Closed 4 years ago.
I am having string as below and want prefix to be removed [*TESTABC*]
String a = "[*TESTABC*]test#test.com";
Expected result: test#test.com
I tried below code but it is removing the bracklets only. I need to remove the inner content also.
The string with come in this pattern only. Please help.
String s = "[*TESTABC*]test#test.com";
String regex = "\\[|\\]";
s = s.replaceAll(regex, "");
System.out.println(s); //*TESTABC*test#test.com
int index;
String s = "[*TESTABC*]test#test.com";
index=s.indexOf(']');
s=s.substring(index+1);
System.out.println(s);
**i solve your question according to your problem and output **

Python: replace Japanese word with brace, such as {keyword: 部屋}, {keyword: 公園}, ..... with 'keyword' [duplicate]

This question already has answers here:
Match text between two strings with regular expression
(3 answers)
Closed 4 years ago.
there are some Japanese sentences like following:
{keyword: 部屋}いいね!
{keyword: 公園}は綺麗です.
私は{keyword: 部屋捜査}です。
   ..........
.........
I want to replace the substring like :{keyword: 部屋},{keyword: 公園}..... with 'keyword'.
For example:
input: 私は{keyword: 部屋捜査}です
output: 私はkeywordです
My trying code is following and but it is wrong, the result is same:
import re
s = '{keyword: 賃貸}'
t = re.sub(r"\{keyword:[あ-んア-ン一-]+\}", 'keyword', s)
print(t)
Thanks!
Use the following:-
inputString = "私は{keyword: 部屋捜査}です"
t = re.sub(r"\{keyword:[^}]*}", 'keyword', inputString)
print(t)

Regex - how to get a matched string excluding all groups? [duplicate]

This question already has answers here:
Replace text inside of square brackets
(3 answers)
Closed 5 years ago.
I'm trying to use regex to filter out all parts of a string, enclosed in brackets.
For instance, having input:
data1 data2[comment] data3[comment] data4 data5[comment][comment]
I want to get:
data1 data2 data3 data4 data5
Is it possible to achieve it with regex?
You can use following regex to match everying that's enclosed within brackets (including square brackets):
(\[[^\]]*\])
DEMO: https://regex101.com/r/7HLQkK/1
Java example:
String string = "data1 data2[comment] data3[comment] data4 data5[comment][comment]";
string = string.replaceAll("(\\[[^\\]]*\\])", "");
and javascript:
var string = "data1 data2[comment] data3[comment] data4 data5[comment][comment]";
string = string.replace(/(\[[^\]]*\])/g, "");
console.log(string);

Erase all occurences of a word from a string [duplicate]

This question already has answers here:
Replace part of a string with another string
(17 answers)
Closed 7 years ago.
I have a string such as:
AAAbbbbbAAA
I'd like to remove all the occurances of a pattern AAA to get:
bbbbb
The pattern can occur anywhere in the string.
Given:
string yourstring("AAAbbbAAA");
string removestring("AAA");
You could simply run something like this multiple times on your string:
yourstring.erase(yourstring.find(removestring), removestring.length());
Of course you will have to check that string::find actually finds an occurence before using string::erase.
Here is the code. It's not very much efficient, but works well, and is a tiny code.
string h = "AAAbbbAAAB";
int pos = h.find("AAA");
while(pos!=-1)
{
h.replace(pos, 3, "");
pos = h.find("AAA");
}
cout << h << endl;
It only works if you know the pattern. If it doesn't what you want, maybe you're looking for a pattern matching algorithm, like KMP.

spliting in c++ like what we do in c# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to split a string in C++?
Splitting a C++ std::string using tokens, e.g. “;”
think I have this string :
string a = "hello,usa,one,good,bad";
I want to splitting this string with ,
so I need a array of string like here :
string *a ; a = { hello , usa , one , good , bad }
what shoudl I do ?
This simple AXE parser will do it:
std::vector<std::string> strings;
auto split = *(*(axe::r_any() - ',') >> e_push_back(strings));
split(a.begin(), a.end());
If you really don't want to code this on your own you can do a web search for "c++ tokenize string" and take, for example, a look here: CPPHOWTO