-1

I have this text:

"('John', 0.55)\n"

regex: [^\n]([A-z]+)

and would like to only keep the name John (no quotes or whatsoever). I managed to obtain John, but I also get the \n unfortunately: http://www.regextester.com/?fam=98328

How can I remove the latter?

Thanks

gihidoma
  • 43
  • 1
  • 6
  • The `[A-Za-z]+` is enough. Note that [`[A-z]` matches more than just letters](http://stackoverflow.com/a/29771926/3832970). Your string has a newline, not `\n` (your regex fiddle is wrong). – Wiktor Stribiżew Aug 12 '17 at 09:00
  • @WiktorStribiżew I still get that 'n' at the end – gihidoma Aug 12 '17 at 09:03
  • @WiktorStribiżew on a sidenote I usually write [A-z], which already seems to capture *all* capitals and small letters, How does [A-Za-z] differ? http://www.regextester.com/?fam=98329 – gihidoma Aug 12 '17 at 09:11
  • As explained in the linked answer, `[A-z]` also captures non-letters which have ASCII codes between `Z` and `a` while `[A-Za-z]` captures *only* letters. – zett42 Aug 12 '17 at 09:35

3 Answers3

1

On my machine uqing Qt (as you indicated C++)

[A-Za-z]+ returned John

QRegExp re("[A-Za-z]+");
QString a = "('John', 0.55)\n";

int pos = re.indexIn(a);
QStringList list = re.capturedTexts();
qDebug()<<"list: "<<list;

Returns: list: ("John")

0

You can use this:

([a-zA-Z0-9]+)(?:.|\\r?\\n)*
d_void
  • 123
  • 10
0

If the string starts always with "(' you could use this one:

(?<=(?:["(']{3}))[A-Za-z0-9_]+
leoinstack
  • 26
  • 3