从字符串中删除所有非单词字符

时间:2018-05-29 14:47:27

标签: java regex string

我想从字符串中删除所有非单词字符,但想要保留空格。 例如:

home/ttt/Desktop/GolemProject/Fileuploads/user_wqe/vbbfgsfdgfds.zip
Internal Server Error: /callgolem/
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ttt/Desktop/GolemProject/callgolem/views.py", line 65, in index
    instance.save()
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1283, in execute_sql
    for sql, params in self.as_sql():
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1236, in as_sql
    for obj in self.query.objs
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1236, in <listcomp>
    for obj in self.query.objs
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1235, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1185, in pre_save_val
    return field.pre_save(obj, add=True)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py", line 287, in pre_save
    file.save(file.name, file.file, save=False)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py", line 86, in save
    name = self.field.generate_filename(self.instance, name)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/files.py", line 306, in generate_filename
    return self.storage.generate_filename(filename)
  File "/usr/local/lib/python3.5/dist-packages/django/core/files/storage.py", line 97, in generate_filename
    dirname, filename = os.path.split(filename)
  File "/usr/lib/python3.5/posixpath.py", line 103, in split
    i = p.rfind(sep) + 1
AttributeError: 'NoneType' object has no attribute 'rfind'

预期产出:

  

Graeme O'Connor Claire N MacDermott Julia K CatherineBryantJosèôGil

但我得到了

  

Graeme O Connor Claire N MacDermott Julia K CatherineBryantJosèôGil

2 个答案:

答案 0 :(得分:3)

替换非单词和非空格以及非'字符,然后将多个空格压缩为一个:

String b = a.replaceAll("[^[\\p{L}\\s']]+", "").replaceAll("\\s+", " ");

输出:

replaced: Graeme O'Connor Dasha Nicholls Claire N MacDermott Julia K Catherine Bryant Josèô Gil

答案 1 :(得分:2)

尝试这样的事情:

    String a = "Graeme O'Connor Dasha Nicholls  Claire N MacDermott1 • Julia K Catherine Bryant1 Josèô Gil2";
    String b = a.replaceAll(".*?(\\b[\\p{L}']+)[^\\p{L}']*", "$1 ");
    System.out.println("replaced: " + b);

打印:

replaced: Graeme O'Connor Dasha Nicholls Claire N MacDermott Julia K Catherine Bryant Josèô Gil